Repository navigation
Expand file tree
/
Copy pathRegexMatching.java
More file actions
60 lines (54 loc) · 2.63 KB
/
Copy pathRegexMatching.java
File metadata and controls
60 lines (54 loc) · 2.63 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
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
* Leetcode verified solution
*/
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 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()));
}
}