Skip to content

Commit e7a610c

Browse files
author
Ram swaroop
committed
coded + unit tested
1 parent 2a224f4 commit e7a610c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package me.ramswaroop.strings;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
*
6+
* @author: ramswaroop
7+
* @date: 10/20/15
8+
* @time: 1:15 PM
9+
*/
10+
public class SubString {
11+
12+
/**
13+
* Naive approach to determine whether string {@param s2} is a
14+
* substring of string {@param s1}.
15+
*
16+
* @param s1
17+
* @param s2
18+
* @return
19+
*/
20+
public static boolean isSubString(String s1, String s2) {
21+
char[] c1 = s1.toCharArray(),
22+
c2 = s2.toCharArray();
23+
int l1 = c1.length,
24+
l2 = c2.length,
25+
i, j;
26+
27+
for (i = 0; i <= l1 - l2; i++) {
28+
for (j = 0; j < l2 && i + j < l1; j++) {
29+
if (c1[i + j] != c2[j]) break;
30+
}
31+
if (j == l2) {
32+
return true;
33+
}
34+
}
35+
return false;
36+
}
37+
38+
public static void main(String a[]) {
39+
System.out.println(isSubString("ramswaroop", "ram"));
40+
System.out.println(isSubString("ramswaroop", "rams"));
41+
System.out.println(isSubString("ramswaroop", "ramss"));
42+
System.out.println(isSubString("ramswaroop", "ar"));
43+
}
44+
}

0 commit comments

Comments
 (0)