Skip to content

Commit 7585972

Browse files
authored
Create StringSearching.java
1 parent 6b03175 commit 7585972

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Hard-Level/StringSearching.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
You are given two strings. Determine if the second string is a substring of the first (Do NOT use any substr type library function). The second string may contain an asterisk(*) which should be treated as a regular expression i.e. matches zero or more characters. The asterisk can be escaped by a \ char in which case it should be interpreted as a regular '*' character. To summarize: the strings can contain alphabets, numbers, * and \ characters.
3+
*/
4+
5+
import java.io.*;
6+
public class Main {
7+
public static void main (String[] args) throws IOException {
8+
File file = new File(args[0]);
9+
BufferedReader br = new BufferedReader(new FileReader(file));
10+
String line;
11+
while ((line = br.readLine()) != null) {
12+
String[] a = line.split(",");
13+
String frstw = a[0];
14+
String lastw = a[1];
15+
int last = lastw.length();
16+
boolean b = false;
17+
if(lastw.contains("*") && !lastw.contains("\\*") && lastw.charAt(last-1)!='*') {
18+
String[] ast = lastw.split("\\*");
19+
String fw = ast[0];
20+
String lw = ast[1];
21+
if (frstw.matches(".*"+fw+".*"+lw+".*")) {
22+
b = true;
23+
}
24+
}
25+
else if (lastw.charAt(last-1)=='*' && lastw.length()>1) {
26+
String[] ast = lastw.split("\\*");
27+
String fw = ast[0];
28+
if (frstw.matches(".*"+fw+".+")) {
29+
b = true;
30+
}
31+
}
32+
else if (lastw.charAt(last-1)=='*' && lastw.length()==1) {
33+
b = true;
34+
}
35+
else if ( lastw.contains("\\*")) {
36+
lastw = lastw.replace("\\", "");
37+
if (frstw.contains(lastw)) {
38+
b = true;
39+
}
40+
}
41+
else {
42+
if (frstw.contains(lastw)) {
43+
b = true;
44+
}
45+
}
46+
System.out.println(b);
47+
}
48+
br.close();
49+
}
50+
}

0 commit comments

Comments
 (0)