首页 > 代码库 > leetcode Regular Expression Matching
leetcode Regular Expression Matching
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).The function prototype should be:bool isMatch(const char *s, const char *p)Some examples:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa") → falseisMatch("aa", "a*") → trueisMatch("aa", ".*") → trueisMatch("ab", ".*") → trueisMatch("aab", "c*a*b") → true
1 public class Solution { 2 public boolean isMatch(String s, String p) { 3 if (s.equals(p)) { 4 return true; 5 } 6 7 int lenS=s.length(); 8 int lenP=p.length(); 9 if (lenP==0) {10 return lenS==0;11 }12 if (lenP==1) {13 if (p.equals(s)||(p.equals(".")&&lenS==1)) {14 return true;15 }else {16 return false;17 }18 }19 20 if (p.charAt(1)!=‘*‘) {21 if (lenS>022 &&(s.charAt(0)==p.charAt(0)||p.charAt(0)==‘.‘)) {23 return isMatch(s.substring(1), p.substring(1));24 }25 return false;26 }else {27 while (s.length()>0&&28 (s.charAt(0)==p.charAt(0)||p.charAt(0)==‘.‘)) {29 if (isMatch(s, p.substring(2))) {30 return true;31 }32 s=s.substring(1);33 }34 return isMatch(s, p.substring(2));35 }36 }37 }
leetcode Regular Expression Matching
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。