Skip to content

Commit 0bb7db2

Browse files
siddhant2002siriak
andauthored
Add anagrams (TheAlgorithms#2859)
Co-authored-by: Andrii Siriak <[email protected]>
1 parent 66d8d51 commit 0bb7db2

File tree

1 file changed

+150
-0
lines changed

1 file changed

+150
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
/** Author : Siddhant Swarup Mallick
2+
* Github : https://github.com/siddhant2002
3+
*/
4+
5+
/** PROBLEM DESCRIPTION :
6+
* 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
7+
*/
8+
9+
package com.thealgorithms.strings;
10+
import java.util.*;
11+
public class Anagrams
12+
{
13+
// 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but differ in running time.
14+
public static void main(String args[]) {
15+
String first = "deal";
16+
String second = "lead";
17+
// All the below methods takes input but doesn't return any output to the main method.
18+
Anagrams nm=new Anagrams();
19+
System.out.println(nm.approach2(first, second)); /* To activate methods for different approaches*/
20+
System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/
21+
System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/
22+
System.out.println(nm.approach4(first, second)); /* To activate methods for different approaches*/
23+
24+
/**
25+
* OUTPUT :
26+
* first string ="deal" second string ="lead"
27+
* Output: Anagram
28+
* Input and output is constant for all four approaches
29+
* 1st approach Time Complexity : O(n logn)
30+
* Auxiliary Space Complexity : O(1)
31+
* 2nd approach Time Complexity : O(n)
32+
* Auxiliary Space Complexity : O(1)
33+
* 3rd approach Time Complexity : O(n)
34+
* Auxiliary Space Complexity : O(1)
35+
* 4th approach Time Complexity : O(n)
36+
* Auxiliary Space Complexity : O(n)
37+
*/
38+
}
39+
40+
boolean approach1(String s, String t)
41+
{
42+
if (s.length() != t.length())
43+
{
44+
return false;
45+
}
46+
else
47+
{
48+
char c[] = s.toCharArray();
49+
char d[] = t.toCharArray();
50+
Arrays.sort(c);
51+
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 */
52+
if (Arrays.equals(c, d))
53+
{
54+
return true;
55+
} else
56+
{
57+
return false;
58+
}
59+
}
60+
}
61+
62+
boolean approach2(String a, String b)
63+
{
64+
if(a.length()!=b.length())
65+
{
66+
return false;
67+
}
68+
else
69+
{
70+
int m[]=new int[26];
71+
int n[]=new int[26];
72+
for(char c: a.toCharArray())
73+
{
74+
m[c-'a']++;
75+
}
76+
// 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
77+
// Running time and space complexity of this algo is less as compared to others
78+
for(char c:b.toCharArray())
79+
{
80+
n[c-'a']++;
81+
}
82+
for(int i=0;i<26;i++)
83+
{
84+
if(m[i]!=n[i])
85+
{
86+
return false;
87+
}
88+
}
89+
return true;
90+
}
91+
}
92+
93+
boolean approach3(String s, String t)
94+
{
95+
if(s.length()!=t.length())
96+
{
97+
return false;
98+
}
99+
// this is similar to approach number 2 but here the string is not converted to character array
100+
else
101+
{
102+
int a[]=new int[26];
103+
int b[]=new int[26];
104+
int k=s.length();
105+
for(int i=0;i<k;i++)
106+
{
107+
a[s.charAt(i)-'a']++;
108+
b[t.charAt(i)-'a']++;
109+
}
110+
for(int i=0;i<26;i++)
111+
{
112+
if(a[i]!=b[i])
113+
return false;
114+
}
115+
return true;
116+
}
117+
}
118+
119+
boolean approach4(String s, String t)
120+
{
121+
if(s.length()!=t.length())
122+
{
123+
return false;
124+
}
125+
// 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
126+
else
127+
{
128+
HashMap<Character,Integer> nm=new HashMap<>();
129+
HashMap<Character,Integer> kk=new HashMap<>();
130+
for(char c: s.toCharArray())
131+
{
132+
nm.put(c, nm.getOrDefault(c,0)+1);
133+
}
134+
for(char c: t.toCharArray())
135+
{
136+
137+
kk.put(c, kk.getOrDefault(c,0)+1);
138+
}
139+
// It checks for equal frequencies
140+
for(char c:nm.keySet())
141+
{
142+
if(!nm.get(c).equals(kk.get(c)))
143+
{
144+
return false;
145+
}
146+
}
147+
return true;
148+
}
149+
}
150+
}

0 commit comments

Comments
 (0)