File tree 1 file changed +44
-0
lines changed
src/me/ramswaroop/strings
1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments