Created
June 7, 2013 15:12
-
-
Save maydayco/5729982 to your computer and use it in GitHub Desktop.
Wildcard Matching
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
| public class Solution { | |
| public boolean isMatch(String s, String p) { | |
| // Start typing your Java solution below | |
| // DO NOT write main() function | |
| boolean[][] sol = new boolean[2][p.length() + 1]; | |
| sol[0][0] = true; | |
| int len = 0; | |
| for (int i = 1; i <= p.length(); i++) { | |
| if (p.charAt(i - 1) == '*') | |
| sol[0][i] = sol[0][i - 1]; | |
| else | |
| len++; | |
| } | |
| if (len > s.length()) | |
| return false; | |
| int cur = 0; | |
| int pre = 1; | |
| for (int i = 1; i <= s.length(); i++) { | |
| cur ^= 1; | |
| pre ^= 1; | |
| for (int j = 1; j <= p.length(); j++) { | |
| if (p.charAt(j - 1) == '?' || p.charAt(j - 1) == s.charAt(i - 1)) { | |
| sol[cur][j] = sol[pre][j - 1]; | |
| } else if (p.charAt(j - 1) == '*') { | |
| sol[cur][j] = sol[pre][j] || sol[cur][j - 1]; | |
| } else | |
| sol[cur][j] = false; | |
| } | |
| sol[cur][0] = false; | |
| } | |
| return sol[cur][p.length()]; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment