Skip to content

Instantly share code, notes, and snippets.

@adohe-zz
Created July 17, 2014 09:59
Show Gist options
  • Select an option

  • Save adohe-zz/7b6a6dadf0612fa62708 to your computer and use it in GitHub Desktop.

Select an option

Save adohe-zz/7b6a6dadf0612fa62708 to your computer and use it in GitHub Desktop.
Regular expression implementation in java
package com.westudio.java;
public class LeetCodeSix {
private static boolean isMatch(String s, String p) {
if (p.length() == 0) {
return s.length() == 0;
}
if (p.length() == 1 || p.charAt(1) != '*') {
if (s.length() < 1 || (p.charAt(0) != s.charAt(0) && p.charAt(0) != '.')) {
return false;
}
return isMatch(s.substring(1), p.substring(1));
} else {
int len = s.length();
int i = -1;
while (i < len && (i < 0 || p.charAt(0) == '.' || s.charAt(i) == p.charAt(0))) {
if (isMatch(s.substring(i + 1), p.substring(2))) {
return true;
}
i ++;
}
return false;
}
}
public static void main(String[] args) {
String s = "aab";
String p = "c*a*b";
System.out.println(isMatch(s, p));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment