* An anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
* typically using all the original letters exactly once.[1]
* For example, the word anagram itself can be rearranged into nag a ram,
* also the word binary into brainy and the word adobe into abode.
* Reference from https://en.wikipedia.org/wiki/Anagram
*/
publicclassAnagrams {
// 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but differ in running time.
publicstaticvoidmain(String[] args) {
Stringfirst = "deal";
Stringsecond = "lead";
// All the below methods takes input but doesn't return any output to the main method.
Anagramsnm = newAnagrams();
System.out.println(nm.approach2(first, second)); /* To activate methods for different approaches*/
System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/
System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/
System.out.println(nm.approach4(first, second)); /* To activate methods for different approaches*/
/**
* OUTPUT :
* first string ="deal" second string ="lead"
* Output: Anagram
* Input and output is constant for all four approaches
* 1st approach Time Complexity : O(n logn)
* Auxiliary Space Complexity : O(1)
* 2nd approach Time Complexity : O(n)
* Auxiliary Space Complexity : O(1)
* 3rd approach Time Complexity : O(n)
* Auxiliary Space Complexity : O(1)
* 4th approach Time Complexity : O(n)
* Auxiliary Space Complexity : O(n)
*/
}
booleanapproach1(Strings, Stringt) {
if (s.length() != t.length()) {
returnfalse;
} else {
charc[] = s.toCharArray();
chard[] = t.toCharArray();
Arrays.sort(c);
Arrays.sort(d); /* In this approach the strings are stored in the character arrays and both the arrays are sorted. After that both the arrays are compared for checking anangram */
if (Arrays.equals(c, d)) {
returntrue;
} else {
returnfalse;
}
}
}
booleanapproach2(Stringa, Stringb) {
if (a.length() != b.length()) {
returnfalse;
} else {
intm[] = newint[26];
intn[] = newint[26];
for (charc : a.toCharArray()) {
m[c - 'a']++;
}
// In this approach the frequency of both the strings are stored and after that the frequencies are iterated from 0 to 26(from 'a' to 'z' ). If the frequencies match then anagram message is displayed in the form of boolean format
// Running time and space complexity of this algo is less as compared to others
for (charc : b.toCharArray()) {
n[c - 'a']++;
}
for (inti = 0; i < 26; i++) {
if (m[i] != n[i]) {
returnfalse;
}
}
returntrue;
}
}
booleanapproach3(Strings, Stringt) {
if (s.length() != t.length()) {
returnfalse;
}
// this is similar to approach number 2 but here the string is not converted to character array
else {
inta[] = newint[26];
intb[] = newint[26];
intk = s.length();
for (inti = 0; i < k; i++) {
a[s.charAt(i) - 'a']++;
b[t.charAt(i) - 'a']++;
}
for (inti = 0; i < 26; i++) {
if (a[i] != b[i])
returnfalse;
}
returntrue;
}
}
booleanapproach4(Strings, Stringt) {
if (s.length() != t.length()) {
returnfalse;
}
// This approach is done using hashmap where frequencies are stored and checked iteratively and if all the frequencies of first string match with the second string then anagram message is displayed in boolean format