-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCheckSubstring.java
More file actions
40 lines (23 loc) · 851 Bytes
/
CheckSubstring.java
File metadata and controls
40 lines (23 loc) · 851 Bytes
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
/*--Check if B is a substring of A or not, if yes then return the index of first occurance!--*/
public class CheckSubstring {
int subString(String A, String B) {
if(A.length()==0 || B.length()==0)
return -1;
if(A.length()==B.length() && A.equals(B))
return 0;
HashMap<Integer, String> hm = new HashMap<>();
for(int i=0; i<(A.length() - B.length()); i++){
hm.put(i, A.substring(i, i + B.length()));
}
try{
for(int i=0; i<hm.size(); i++){
if(hm.get(i).equals(B))
return i;
}
}
catch(Exception e){
return -1;
}
return -1;
}
}