LeetCode10 regular expressions

After completion of study section has been very impetuous, nor Zuosha thing, the epidemic is now so serious, not out of the house, the initial publication of the results also postponed, as well as ten days to go out the results, and call ...... do first complete set up to write algorithms , after a score and then going to lie.

10 Regular Expressions

Topic Link

Submitted before trying this problem, but did not before, make up today.

There are two ways to solve problems reported, backtracking, dynamic programming. Write your own time with backtracking, but its code is not simple and is not considered comprehensive. Problem-solving report was backtracking code is really refreshing.

Backtracking

If there is no sign of this title * will be very simple, before a character zero or more times * symbol represents the matching addition. Notation with any character.

As a symbol * There are two cases:

  • Matches the preceding character zero
  • Matches the preceding character multiple times

Therefore, partial situation :( i, j indicate a position of s, p string)

  1. p[j+1] != '*' p[i] == s[i] || p[j] == '.'
    That is s [i] p [j] matches, and p [j] is not behind the characters *: Order s [i + 1] and p [j + 1] to match string behind
  2. p[j+1] == '*'
    This time we are two circumstances,
    when the Match 0: to compare s [i] and p [j 2 +] after string, if the matching is successful
    matching when a plurality of times: s [i] == p [j] and after the string s [i + 1] and p [j] matches successfully
Code:
	public boolean isMatch(String s, String p) {
        if (p.isEmpty()) return s.isEmpty(); //两者为“” 则为true

		// 第一个字符是否匹配
        boolean first = (!s.isEmpty()) && (s.charAt(0) == p.charAt(0) || p.charAt(0) == '.');

        if (p.length() >= 2 && p.charAt(1) == '*') {//有*的时候
            // || 前面为匹配0次的情况 “aab, a*c*b” 
            // || 后面为匹配多次的情况 “aaab, a*b”
            return (isMatch(s, p.substring(2))) || (first && isMatch(s.substring(1), p));
        } else {
            return first && isMatch(s.substring(1), p.substring(1));
        }
    }

Dynamic Programming

By dp[i][j]indicating whether the characters before the s i can be matched before the j-th character p success.
According dp[i-1][j-1]to judgedp[i][j]

  1. p[j] != '*'
    p[j] == s[i] || p[j] == '.' : p[i][j] = p[i-1][j-1]
  2. p[j] == '*'
    p[j-1] != s[i]: s[i]And *in front of the characters do not match on, but it does not mean that the match fails, there may be before the character * Match 0, which is the need to look at dp[i][j-2]the situation: dp[i][j] = dp[i][j-2]
    p[j-1] == s[i]: with *the matching characters before, but this is not representative of the s i may be before the first j characters matching characters p, in which case or two cases: match 0, i.e., dp[i][j-2]a case; multiple matching, i.e., dp[i-1][j]a case; both cases there is a successful, i.e. a successful
Code:
    public boolean isMatch(String s, String p) {
        boolean[][] dp = new boolean[s.length()+1][p.length()+1];
        dp[0][0] = true;
        // 相当于初始 "" 被p匹配的情况
        for (int i = 0; i < p.length(); i++) { 
            if (p.charAt(i)=='*' && dp[0][i-1]) dp[0][i+1] = true;
        }
		
        for (int i = 0; i < s.length(); i++) {
            for (int j = 0; j < p.length(); j++) {
                //  s[i] 可以和 p[j] 匹配的情况,看dp前面
                if (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'){
                    dp[i+1][j+1] = dp[i][j];
                }
                if (p.charAt(j) == '*'){
                	// s[i] 和 p[j] 前的字符匹配不上 考虑匹配0次的情况
                    if (p.charAt(j-1) != s.charAt(i) && p.charAt(j-1) != '.'){
                        dp[i+1][j+1] = dp[i+1][j-1];
                    }else{
                    //s[i] 和 p[j] 前的字符匹配上 需要考虑匹配0次和匹配多次的情况 
                        dp[i+1][j+1] = dp[i+1][j-1] || dp[i][j+1];
                    }
                }
            }
        }

        return dp[s.length()][p.length()];
    }
代码中的dp[i][j] 和 s[i] p[j] 中的i和j分别代表各自在各种中的索引
发布了151 篇原创文章 · 获赞 43 · 访问量 10万+

Guess you like

Origin blog.csdn.net/qq_38595487/article/details/104254718