forked from 14visheshjain/DataStructure-Algorithm-in-java
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwildCardMatching.java
More file actions
50 lines (45 loc) · 1.48 KB
/
wildCardMatching.java
File metadata and controls
50 lines (45 loc) · 1.48 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
/*
* @author Vishesh Jain
* @date 28-March-2019
*/
package lecture9a10;
//Given a text and a wildcard pattern, implement wildcard pattern matching algorithm that finds if wildcard pattern is matched with text. The matching should cover the entire text (not partial text).
//
//The wildcard pattern can include the characters ‘?’ and ‘*’
//‘?’ – matches any single character
//‘*’ – Matches any sequence of characters (including the empty sequence)
import java.util.Scanner;
import javax.rmi.ssl.SslRMIClientSocketFactory;
public class wildCardMatching {
public static void main(String[] args) {
Scanner a = new Scanner(System.in);
String src = a.next();
String pattern = a.next();
System.out.println(wildCardMatching(src, pattern));
}
public static boolean wildCardMatching(String src, String pattern) {
if (src.length() == 0 && pattern.length() == 0)
return true;
if (src.length() != 0 && pattern.length() == 0)
return false;
if (src.length() == 0 && pattern.length() != 0) {
for (int i = 0; i < pattern.length(); i++) {
if (pattern.charAt(i) != '*')
return false;
}
return true;
}
char srcch = src.charAt(0);
char patternch = pattern.charAt(0);
String ros = src.substring(1);
String rop = pattern.substring(1);
boolean rr;
if (srcch == patternch || patternch == '?') {
rr = wildCardMatching(ros, rop);
} else if (patternch == '*') {
rr = wildCardMatching(src, rop) || wildCardMatching(ros, pattern);
} else
return false;
return rr;
}
}