-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFindSubString.java
More file actions
38 lines (35 loc) · 925 Bytes
/
FindSubString.java
File metadata and controls
38 lines (35 loc) · 925 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
/**
Find sub-string in a string without using any Library Method of String
**/
public class Solution {
// DO NOT MODIFY THE ARGUMENTS WITH "final" PREFIX. IT IS READ ONLY
public int strStr(final String A, final String B) {
if (A == null || B ==null || A.isEmpty() || B.isEmpty())
{
return -1;
}
if(A.length() < B.length()){
return -1;
}
if(A.length() == B.length()){
if(A.equals(B)){
return 0;
}
else return -1;
}
for(int i =0; i<A.length()-B.length(); i++)
{
int count = 0;
for(int j=0; j<B.length(); j++)
{
if(A.charAt(i+j)==B.charAt(j)){
count++;
}
}
if(count==B.length()){
return i;
}
}
return -1;
}
}