Created
July 17, 2014 09:59
-
-
Save adohe-zz/7b6a6dadf0612fa62708 to your computer and use it in GitHub Desktop.
Regular expression implementation in java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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