Repository navigation
Expand file tree
/
Copy pathRegexMatching.java
More file actions
88 lines (78 loc) · 3.57 KB
/
Copy pathRegexMatching.java
File metadata and controls
88 lines (78 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.interview.string;
/**
* Date 06/24/2015
* @author tusroy
*
* Write a program to perform regex matching with * an .
*
* References : http://leetcode.com/2011/09/regular-expression-matching.html
*/
public class RegexMatching {
public boolean match(char[] str, char[] pattern){
return match(str,pattern,0,0);
}
private boolean match(char text[], char pattern[], int pos1, int pos2){
//if pos2 has reached end of pattern means pos2 should also reach end of text for match
//to happen
if(pos2 == pattern.length) {
return pos1 == text.length;
}
//if next character is not * means either current value at pattern and text should be same
//or current value at pattern should be . in which case you can skip one character of text
if(pos2 == pattern.length - 1 || pattern[pos2+1] != '*') {
return (pos1 < text.length && (text[pos1] == pattern[pos2] || pattern[pos2] == '.')) && match(text, pattern, pos1+1, pos2+1);
}
//if we have case like abc and ad*bc so here we totally skip d*
if(match(text, pattern, pos1, pos2+2)){
return true;
}
//For case like abbc and ab*c match first b with b* and then next b to c. If that does not work out
//then try next b with b* and then c with c and so on.
//if pattern current val is . then skip one character at time from text till we either reach end of text
//or a match is found
while(pos1 < text.length && (text[pos1] == pattern[pos2] || pattern[pos2] == '.')){
if( match(text, pattern, pos1+1, pos2+2)){
return true;
}
pos1++;
}
return false;
}
public boolean matchDP(char[] str, char[] pattern) {
boolean T[][] = new boolean[str.length + 1][pattern.length + 1];
T[0][0] = true;
for (int i = 1; i < T[0].length; i++) {
if (pattern[i-1] == '*') {
T[0][i] = T[0][i - 2];
}
}
for (int i = 1; i < T.length; i++) {
for (int j = 1; j < T[0].length; j++) {
if (pattern[j-1] != '*') {
if (pattern[j - 1] == '.' || pattern[j - 1] == str[i - 1]) {
T[i][j] = T[i-1][j-1];
}
} else {
if (pattern[j-2] == '.' || pattern[j - 2] == str[i - 1]) {
T[i][j] = T[i - 1][j];
}
T[i][j] = T[i][j] | T[i][j-2];
}
}
}
return T[str.length][pattern.length];
}
public static void main(String args[]){
RegexMatching rm = new RegexMatching();
System.out.println(rm.match("Tushar".toCharArray(),"Tushar".toCharArray()));
System.out.println(rm.match("Tusha".toCharArray(),"Tushar*a*b*".toCharArray()));
System.out.println(rm.match("".toCharArray(),"a*b*".toCharArray()));
System.out.println(rm.match("abbbbccc".toCharArray(),"a*ab*bbbbc*".toCharArray()));
System.out.println(rm.match("abbbbccc".toCharArray(),"aa*bbb*bbbc*".toCharArray()));
System.out.println(rm.match("abbbbccc".toCharArray(),".*bcc".toCharArray()));
System.out.println(rm.match("abbbbccc".toCharArray(),".*bcc*".toCharArray()));
System.out.println(rm.match("abbbbccc".toCharArray(),".*bcc*".toCharArray()));
System.out.println(rm.match("aaa".toCharArray(),"ab*a*c*a".toCharArray()));
System.out.println(rm.matchDP("aa".toCharArray(), "a*".toCharArray()));
}
}