forked from sPredictorX1708/Ultimate-Java-Resources
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanagram.java
More file actions
60 lines (51 loc) · 1.92 KB
/
anagram.java
File metadata and controls
60 lines (51 loc) · 1.92 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Arrays;
public class Anagram {
/**
Given an array of strings to the method isAnagram(@param String[])
compares the strings and finds out if the string is anagram to the other string in the same array.
*/
// Time Complexity: O(nLogn)
public static void isAnagram(String[] arr){
char[][] chars = new char[arr.length][256];
for (int i = 0; i < arr.length; i++) {
chars[i] = arr[i].toCharArray();
Arrays.sort(chars[i]);
//System.out.println(chars[i]);
}
for (int i=0; i<arr.length;i++){
for (int j = i+1; j < arr.length; j++) {
if (Arrays.toString(chars[i]).compareToIgnoreCase(Arrays.toString(chars[j])) == 0){
System.out.println(i+1 + ": " + arr[i] + " is anagram to " + arr[j]);
}
}
}
}
/**
areAnagram(@param String, @param String) takes two strings and returns a boolean value whether the passed strings
are anagram or not.
*/
public static boolean areAnagram(String str1, String str2){
if (str1.length() != str2.length()) return false;
//XOR value
int value = 0;
for (int i=0; i<str1.length(); i++){
value = value ^ (int) str1.charAt(i);
value = value ^ (int) str2.charAt(i);
}
return value == 0;
}
public static void main(String[] args) {
String[] s = {"geeksquiz", "geeksforgeeks", "abcd",
"forgeeksgeeks", "zuiqkeegs", "Kavin Raju", "Raju Kavin"};
System.out.println("Is anagram:");
isAnagram(s);
System.out.println("\nAre anagram");
for (int i=0;i<s.length;i++){
for (int j = i+1; j < s.length; j++) {
if (areAnagram(s[i],s[j])){
System.out.println(s[i] + " and " + s[j] + " are angram.");
}
}
}
}
}