Skip to content

Commit a345943

Browse files
Added Modified Regex.
1 parent d4e99e9 commit a345943

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/main/java/interview/amazon/ModifiedRegex.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public static void test() {
3434
assertEquals(true, match("abcde", "....*"));
3535
assertEquals(false, match("abcde", "....f"));
3636
assertEquals(true, match("abcdd", "....*"));
37+
assertEquals(true, match("abcdd", "c*.*"));
3738
}
3839

3940
public static boolean match(String s, String p) {
@@ -46,8 +47,11 @@ public static boolean match(String s, String p, int i, int j) {
4647
if (i >= s.length() || j >= p.length())
4748
return false;
4849
char sc = s.charAt(i), pc = p.charAt(j);
49-
if (j + 1 < p.length() && p.charAt(j + 1) == '*')
50-
return sc == pc || pc == '.' ? match(s, p, i + 1, j) || match(s, p, i + 1, j + 2) : false;
50+
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
51+
if (sc != pc && pc != '.' && match(s, p, i, j + 2))
52+
return true;
53+
return sc == pc || pc == '.' ? (match(s, p, i + 1, j) || match(s, p, i + 1, j + 2)) : false;
54+
}
5155
return sc == pc || pc == '.' ? match(s, p, i + 1, j + 1) : false;
5256
}
5357

0 commit comments

Comments
 (0)