File tree 2 files changed +56
-0
lines changed
src/me/ramswaroop/strings
2 files changed +56
-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/21/15
8
+ * @time: 10:06 AM
9
+ * @see: me.ramswaroop.strings.SubString for a similar problem.
10
+ */
11
+ public class StringRotation {
12
+
13
+ /**
14
+ * Determines if string {@param s2} is a rotation of string {@param s1}.
15
+ *
16
+ * @param s1
17
+ * @param s2
18
+ * @return
19
+ */
20
+ public static boolean isStringRotation (String s1 , String s2 ) {
21
+ char [] c1 = s1 .toCharArray ();
22
+ char [] c2 = s2 .toCharArray ();
23
+
24
+ int l1 = c1 .length ,
25
+ l2 = c2 .length ,
26
+ i , j , k ;
27
+
28
+ for (i = 0 ; i < l1 ; i ++) {
29
+ for (j = 0 ; j < l2 && i + j < l1 ; j ++) {
30
+ if (c1 [i + j ] != c2 [j ]) break ;
31
+ }
32
+ k = 0 ;
33
+ while (k < l1 && j < l2 ) {
34
+ if (c1 [k ++] != c2 [j ]) break ;
35
+ j ++;
36
+ }
37
+ if (j == l2 ) {
38
+ return true ;
39
+ }
40
+ }
41
+ return false ;
42
+ }
43
+
44
+ public static void main (String a []) {
45
+ System .out .println (isStringRotation ("ramswaroop" , "swaroopram" ));
46
+ System .out .println (isStringRotation ("ramswaroop" , "swaroopramramram" ));
47
+ System .out .println (isStringRotation ("ramswaroop" , "mswaroopra" ));
48
+ System .out .println (isStringRotation ("ramswaroop" , "swarooppram" ));
49
+ System .out .println (isStringRotation ("ramswaroop" , "" ));
50
+ System .out .println (isStringRotation ("mswaroopra" , "ramswaroop" ));
51
+ System .out .println (isStringRotation ("amam" , "mama" ));
52
+ }
53
+ }
Original file line number Diff line number Diff line change 6
6
* @author: ramswaroop
7
7
* @date: 10/20/15
8
8
* @time: 1:15 PM
9
+ * @see: me.ramswaroop.strings.StringRotation for a similar problem.
9
10
*/
10
11
public class SubString {
11
12
@@ -36,6 +37,8 @@ public static boolean isSubString(String s1, String s2) {
36
37
}
37
38
38
39
public static void main (String a []) {
40
+ System .out .println (isSubString ("ramswaroop" , "ramswaroop" ));
41
+ System .out .println (isSubString ("ramswaroop" , "" ));
39
42
System .out .println (isSubString ("ramswaroop" , "ram" ));
40
43
System .out .println (isSubString ("ramswaroop" , "rams" ));
41
44
System .out .println (isSubString ("ramswaroop" , "ramss" ));
You can’t perform that action at this time.
0 commit comments