-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnagrams.java
41 lines (30 loc) · 1.08 KB
/
Anagrams.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
36
37
38
39
40
41
import java.util.Arrays;
import java.util.Scanner;
public class Anagrams {
public static boolean areAnagrams(String str1, String str2) {
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
if (str1.length() != str2.length()) {
return false;
}
char[] s1 = str1.toCharArray();
char[] s2 = str2.toCharArray();
Arrays.sort(s1);
Arrays.sort(s2);
return Arrays.equals(s1, s2);
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get input from the user
System.out.print("Enter the first word: ");
String word1 = scanner.nextLine();
System.out.print("Enter the second word: ");
String word2 = scanner.nextLine();
// Check if the words are anagrams
boolean result = areAnagrams(word1, word2);
// Display the result
System.out.println("Are \"" + word1 + "\" and \"" + word2 + "\" anagrams? " + result);
// Close the scanner to prevent resource leak
scanner.close();
}
}