Skip to content

Commit 23e0bbd

Browse files
author
Ram swaroop
committed
coded + unit tested
1 parent 839501a commit 23e0bbd

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
}

src/me/ramswaroop/strings/SubString.java

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* @author: ramswaroop
77
* @date: 10/20/15
88
* @time: 1:15 PM
9+
* @see: me.ramswaroop.strings.StringRotation for a similar problem.
910
*/
1011
public class SubString {
1112

@@ -36,6 +37,8 @@ public static boolean isSubString(String s1, String s2) {
3637
}
3738

3839
public static void main(String a[]) {
40+
System.out.println(isSubString("ramswaroop", "ramswaroop"));
41+
System.out.println(isSubString("ramswaroop", ""));
3942
System.out.println(isSubString("ramswaroop", "ram"));
4043
System.out.println(isSubString("ramswaroop", "rams"));
4144
System.out.println(isSubString("ramswaroop", "ramss"));

0 commit comments

Comments
 (0)