题目描述
Given an input string (s) and a pattern §, 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).
Note:
s could be empty and contains only lowercase letters a-z.
p could be empty and contains only lowercase letters a-z, and characters like . or *.
Example 1:
- Input:
s = “aa”
p = “a”
- Output: false
Explanation: “a” does not match the entire string “aa”.
Example 2:
- Input:
s = “aa”
p = “a*”
- Output: true
Explanation: ‘*’ means zero or more of the precedeng element, ‘a’. Therefore, by repeating ‘a’ once, it becomes “aa”.
Example 3:
- Input:
s = “ab”
p = “.*”
- Output: true
Explanation: “." means "zero or more () of any character (.)”.
Example 4:
- Input:
s = “aab”
p = “cab”
- Output: true
Explanation: c can be repeated 0 times, a can be repeated 1 time. Therefore it matches “aab”.
Example 5:
- Input:
s = “mississippi”
p = “misisp*.”
- Output: false
note: s = “aasdfsadfsdab”, p = “a.*b” 是匹配的
AC代码1
剑指offer解法
class Solution {
public boolean isMatch(String s, String p) {
return match(s.toCharArray(), p.toCharArray());
}
public boolean match(char[] str, char[] pattern) {
if (str == null || pattern == null) {
return false;
}
int strIndex = 0;
int patternIndex = 0;
return matchCore(str, strIndex, pattern, patternIndex);
}
public boolean matchCore(char[] str, int strIndex, char[] pattern, int patternIndex) {
//有效性检验:str到尾,pattern到尾,匹配成功
if (strIndex == str.length && patternIndex == pattern.length) {
return true;
}
//pattern先到尾,匹配失败
if (strIndex != str.length && patternIndex == pattern.length) {
return false;
}
//模式第2个是*,且字符串第1个跟模式第1个匹配,分3种匹配模式;如不匹配,模式后移2位
if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
return matchCore(str, strIndex, pattern, patternIndex + 2)//模式后移2,视为x*匹配0个字符
|| matchCore(str, strIndex + 1, pattern, patternIndex + 2)//视为模式匹配1个字符
|| matchCore(str, strIndex + 1, pattern, patternIndex);//*匹配1个,再匹配str中的下一个
} else {
return matchCore(str, strIndex, pattern, patternIndex + 2);
}
}
//模式第2个不是*,且字符串第1个跟模式第1个匹配,则都后移1位,否则直接返回false
if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
return matchCore(str, strIndex + 1, pattern, patternIndex + 1);
}
return false;
}
}
AC代码2
参考自:leetcode
class Solution {
public boolean isMatch(String s, String p) {
if ((s.length()== 0) && (p.length() == 0)) return true;
//start from end of string and pattern.
return isMatch(s, p, s.length()-1, p.length()-1);
}
public boolean isMatch(String s, String p, int si, int pi) {
//return true, if both are empty (-1 means end of string iteration)
if (pi == -1 && si == -1) return true;
//if pattern is empty, then true if s is empty, else false
if (pi < 0) return si < 0;
//current pattern char
char pc = p.charAt(pi);
//if current pattern char is *
if (pc == '*') {
//pat iter len is >= 2 and str iter is not empty
//current str char matches prev pattern char or
//prev pattern char is '.' and match remaining str char with current patter
//Or skip * and its preceding char
return pi > 0 && si >= 0 &&
((p.charAt(pi-1) == '.' || p.charAt(pi-1) == s.charAt(si))
&& isMatch(s, p, si-1, pi))
|| isMatch(s, p, si, pi-2);
} else return si >= 0 && (pc == '.' || pc == s.charAt(si)) && isMatch(s, p, si-1, pi-1) ;
//return false;
}
}
上述代码是经过 leetcode 中别人的代码优化过了的,效果还不错。