剑指offer-正则表达式匹配

来源:互联网 发布:生死狙击m1911伤害数据 编辑:程序博客网 时间:2024/05/01 12:35

题目描述
请实现一个函数用来匹配包括’.’和’‘的正则表达式。模式中的字符’.’表示任意一个字符,而’‘表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串”aaa”与模式”a.a”和”ab*ac*a”匹配,但是与”aa.a”和”ab*a”均不匹配
解题思路
逐个匹配字符
当模式中的第二个字符不是“*”时:
1、如果字符串第一个字符和模式中的第一个字符相匹配,那么字符串和模式都后移一个字符,然后匹配剩余的。
2、如果 字符串第一个字符和模式中的第一个字符相不匹配,直接返回false。
当模式中第二个字符是“*”时
如果字符串第一个字符跟模式第一个字符不匹配,则模式后移2个字符,继续匹配。如果字符串第一个字符跟模式第一个字符匹配,可以有3种匹配方式:
1、模式后移2字符,相当于x*被忽略;
2、字符串后移1字符,模式后移2字符;
3、字符串后移1字符,模式不变,即继续匹配字符下一位,因为*可以匹配多位;

代码如下:

package match;public class Solution {    public static void main(String[] args) {        char[] str = "aaa".toCharArray();        char[] pattern = "ab*a".toCharArray();        System.out.println(match(str, pattern));    }     public static boolean match(char[] str, char[] pattern)        {            if(str == null || pattern == null){                return false;            }            int strIndex = 0;            int patternIndex = 0;            return matchRegulation(str, strIndex, pattern, patternIndex);        }        public  static boolean matchRegulation(char[] str,int strIndex, char[] pattern, int patternIndex){            //str到尾,pattern到尾,匹配成功            if(strIndex == str.length && patternIndex == pattern.length){                return true;            }            //pattern到尾,匹配失败            if(patternIndex == pattern.length && strIndex != str.length){                return false;            }            //pattern中第一个与str第一个相等且第二个出现*,分三种匹配模式;如不匹配,模式后移2位            if(patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*'){                if((strIndex != str.length && str[strIndex] == pattern[patternIndex])||                   (strIndex != str.length && pattern[patternIndex]=='.')){                    return matchRegulation(str, strIndex, pattern, patternIndex + 2) ||                            matchRegulation(str, strIndex + 1, pattern, patternIndex + 2)                            || matchRegulation(str, strIndex + 1, pattern, patternIndex);                }                else{                    return matchRegulation(str, strIndex, pattern, patternIndex + 2);                }            }            if((strIndex != str.length && str[strIndex] == pattern[patternIndex])||               (strIndex != str.length && pattern[patternIndex]=='.')){                return matchRegulation(str, strIndex + 1, pattern, patternIndex + 1);            }            return false;        }}

解法二:

 public boolean match(char[] str, char[] pattern)  {           return  new String(str).matches(new String(pattern)); } 

注:来自牛客网https://www.nowcoder.com/practice/45327ae22b7b413ea21df13ee7d6429c?tpId=13&tqId=11205&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking