[leetcode]-10 Regular Expression Matching

深藏阁楼爱情的钟 2023-06-29 05:53 126阅读 0赞

描述:

Given an input string (s) and a pattern (p), implement regular expression matching with support for '.' and '*'.

‘.’ Matches any single character. ‘*‘ Matches zero or more of the preceding element.

The matching should cover the entire input string (not partial).

思路:

采用动态规划(Dynamic Programming)的思想,首先先介绍一下动态规划的概念,即把原问题分解成子问题进行求解。

两种不同的动态规划解决方案:

自上而下:你从最顶端开始不断地分解问题,直到你看到问题已经分解到最小并已得到解决,之后只用返回保存的答案即可。这叫做记忆存储(*Memoization*),其使用了递归计数。

自下而上:你可以直接开始解决较小的子问题,从而获得最好的解决方案。在此过程中,你需要保证在解决问题之前先解决子问题。这可以称为表格填充算法(*Tabulation,*table-filling algorithm**),其用到了迭代技术。

在本题中,我们使用一个动态规划矩阵dp[][],原问题是dp[s.length-1][p.length]为true或者false,而若干个子问题为判断dp[i][j]。

我们给出问题之间的关系:

  1. 1, If p.charAt(j+1) == s.charAt(i+1) : dp[i+1][j+1] = dp[i][j];
  2. 2, If p.charAt(j+1) == '.' : dp[i+1][j+1] = dp[i][j];
  3. 3, If p.charAt(j+1) == '*':
  4. here are two sub conditions:
  5. 1 if p.charAt(j) != s.charAt(i+1) and p.charAt(j) != '.':
  6. dp[i+1][j+1] = dp[i+1][j-1] //in this case, a* only counts as empty
  7. 2 if p.charAt(j) == s.charAt(i+1) or p.charAt(i) == '.':
  8. dp[i+1][j+1] = dp[i][j+1] //in this case, a* counts as multiple a
  9. or dp[i+1][j+1] = dp[i+1][j] // in this case, a* counts as single a
  10. or dp[i+1][j+1] = dp[i+1][j-1] // in this case, a* counts as empty

代码:

  1. public boolean isMatch(String s, String p) {
  2. boolean[][] dp = new boolean[s.length()+1][p.length()+1];
  3. int i = 0, j = 0;
  4. //initialize dp matrix
  5. dp[0][0] = true;
  6. for (i = 0; i < p.length(); i++) {
  7. if (p.charAt(i) == '*' && dp[0][i-1]) {
  8. dp[0][i+1] = true;
  9. }
  10. }
  11. //dp by iteration
  12. for(i = 0; i < s.length(); ++i){
  13. for(j = 0; j < p.length(); ++j) {
  14. if (s.charAt(i) == p.charAt(j)) {
  15. dp[i+1][j+1] = dp[i][j];
  16. } else if (p.charAt(j) == '.') {
  17. dp[i+1][j+1] = dp[i][j];
  18. } else if (p.charAt(j) == '*') {
  19. if (p.charAt(j - 1) != s.charAt(i) && p.charAt(j - 1) != '.') {
  20. dp[i+1][j+1] = dp[i+1][j - 1];
  21. } else if (p.charAt(j - 1) == s.charAt(i) || p.charAt(j - 1) == '.') {
  22. dp[i+1][j+1] = dp[i][j+1] || dp[i+1][j] || dp[i+1][j-1];
  23. }
  24. }
  25. }
  26. }
  27. return dp[s.length()][p.length()] ;
  28. }

发表评论

表情:
评论列表 (有 0 条评论,126人围观)

还没有评论,来说两句吧...

相关阅读