-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay21.java
34 lines (26 loc) · 1.08 KB
/
Day21.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
import java.util.Scanner;
public class StringRotationCheck {
public static boolean areRotations(String str1, String str2) {
// Check if both strings have the same length and are not empty
if (str1.length() != str2.length() || str1.isEmpty() || str2.isEmpty()) {
return false;
}
// Concatenate str1 with itself to form a new string
String concatenated = str1 + str1;
// Check if str2 is a substring of the concatenated string
return concatenated.contains(str2);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first string: ");
String str1 = scanner.nextLine();
System.out.print("Enter the second string: ");
String str2 = scanner.nextLine();
if (areRotations(str1, str2)) {
System.out.println(str2 + " is a rotation of " + str1);
} else {
System.out.println(str2 + " is not a rotation of " + str1);
}
scanner.close();
}
}