-
Notifications
You must be signed in to change notification settings - Fork 824
/
Copy pathStringRotation.java
35 lines (31 loc) · 933 Bytes
/
StringRotation.java
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
package misc;
/**
* Algorithm:-
* 1. Create a temp string and store concatenation of str1 to str1 in temp.
* temp = str1.str1
* 2. If str2 is a substring of temp then str1 and str2 are
* rotations of each other.
*
* Example:
* str1 = "ABACD"
* str2 = "CDABA"
*
* temp = str1.str1 = "ABACDABACD"
* Since str2 is a substring of temp, str1 and str2 are
* rotations of each other.
*
*/
public class StringRotation {
static boolean isRotation(String str1, String str2) {
return (str1.length() == str2.length()) && ((str1 + str2).indexOf(str2) != -1);
}
public static void main(String[] args) {
String str1 = "AACD";
String str2 = "ACDA";
if(isRotation(str1, str2)) {
System.out.println("Strings are rotations of each other");
} else {
System.out.println("Strings are not rotations of each other");
}
}
}