Skip to content

Commit 8d099ee

Browse files
aldoteleyanglbme
andauthored
Update Anagrams and add unit test (TheAlgorithms#3002)
* Add test for Anagrams * Update Anagrams.java Co-authored-by: Yang Libin <contact@yanglibin.info>
1 parent 140f6ec commit 8d099ee

File tree

2 files changed

+77
-80
lines changed

2 files changed

+77
-80
lines changed
Lines changed: 56 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
/** Author : Siddhant Swarup Mallick
2-
* Github : https://github.com/siddhant2002
3-
*/
1+
package com.thealgorithms.strings;
42

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-
*/
3+
import java.util.Arrays;
4+
import java.util.HashMap;
85

9-
package com.thealgorithms.strings;
10-
import java.util.*;
11-
public class Anagrams
12-
{
6+
7+
/**
8+
* An anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
9+
* typically using all the original letters exactly once.[1]
10+
* For example, the word anagram itself can be rearranged into nag a ram,
11+
* also the word binary into brainy and the word adobe into abode.
12+
* Reference from https://en.wikipedia.org/wiki/Anagram
13+
*/
14+
public class Anagrams {
1315
// 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[]) {
16+
public static void main(String[] args) {
1517
String first = "deal";
1618
String second = "lead";
1719
// All the below methods takes input but doesn't return any output to the main method.
18-
Anagrams nm=new Anagrams();
20+
Anagrams nm = new Anagrams();
1921
System.out.println(nm.approach2(first, second)); /* To activate methods for different approaches*/
2022
System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/
2123
System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/
@@ -37,114 +39,88 @@ public static void main(String args[]) {
3739
*/
3840
}
3941

40-
boolean approach1(String s, String t)
41-
{
42-
if (s.length() != t.length())
43-
{
42+
boolean approach1(String s, String t) {
43+
if (s.length() != t.length()) {
4444
return false;
45-
}
46-
else
47-
{
45+
} else {
4846
char c[] = s.toCharArray();
4947
char d[] = t.toCharArray();
5048
Arrays.sort(c);
5149
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-
{
50+
if (Arrays.equals(c, d)) {
5451
return true;
55-
} else
56-
{
52+
} else {
5753
return false;
5854
}
5955
}
6056
}
6157

62-
boolean approach2(String a, String b)
63-
{
64-
if(a.length()!=b.length())
65-
{
58+
boolean approach2(String a, String b) {
59+
if (a.length() != b.length()) {
6660
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']++;
61+
} else {
62+
int m[] = new int[26];
63+
int n[] = new int[26];
64+
for (char c : a.toCharArray()) {
65+
m[c - 'a']++;
7566
}
7667
// 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
7768
// 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']++;
69+
for (char c : b.toCharArray()) {
70+
n[c - 'a']++;
8171
}
82-
for(int i=0;i<26;i++)
83-
{
84-
if(m[i]!=n[i])
85-
{
72+
for (int i = 0; i < 26; i++) {
73+
if (m[i] != n[i]) {
8674
return false;
8775
}
8876
}
8977
return true;
9078
}
9179
}
9280

93-
boolean approach3(String s, String t)
94-
{
95-
if(s.length()!=t.length())
96-
{
81+
boolean approach3(String s, String t) {
82+
if (s.length() != t.length()) {
9783
return false;
9884
}
9985
// 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']++;
86+
else {
87+
int a[] = new int[26];
88+
int b[] = new int[26];
89+
int k = s.length();
90+
for (int i = 0; i < k; i++) {
91+
a[s.charAt(i) - 'a']++;
92+
b[t.charAt(i) - 'a']++;
10993
}
110-
for(int i=0;i<26;i++)
111-
{
112-
if(a[i]!=b[i])
94+
for (int i = 0; i < 26; i++) {
95+
if (a[i] != b[i])
11396
return false;
11497
}
11598
return true;
11699
}
117100
}
118101

119-
boolean approach4(String s, String t)
120-
{
121-
if(s.length()!=t.length())
122-
{
123-
return false;
102+
boolean approach4(String s, String t) {
103+
if (s.length() != t.length()) {
104+
return false;
124105
}
125106
// 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);
107+
else {
108+
HashMap<Character, Integer> nm = new HashMap<>();
109+
HashMap<Character, Integer> kk = new HashMap<>();
110+
for (char c : s.toCharArray()) {
111+
nm.put(c, nm.getOrDefault(c, 0) + 1);
133112
}
134-
for(char c: t.toCharArray())
135-
{
136-
137-
kk.put(c, kk.getOrDefault(c,0)+1);
113+
for (char c : t.toCharArray()) {
114+
115+
kk.put(c, kk.getOrDefault(c, 0) + 1);
138116
}
139117
// It checks for equal frequencies
140-
for(char c:nm.keySet())
141-
{
142-
if(!nm.get(c).equals(kk.get(c)))
143-
{
118+
for (char c : nm.keySet()) {
119+
if (!nm.get(c).equals(kk.get(c))) {
144120
return false;
145121
}
146-
}
122+
}
147123
return true;
148124
}
149125
}
150-
}
126+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.thealgorithms.strings;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.*;
6+
7+
public class AnagramsTest {
8+
@Test
9+
public void isAlphabetical() {
10+
String input1 = "late";
11+
Anagrams anagrams = new Anagrams();
12+
assertTrue(anagrams.approach1(input1, "tale"));
13+
assertTrue(anagrams.approach1(input1, "teal"));
14+
assertTrue(anagrams.approach2(input1, "tale"));
15+
assertTrue(anagrams.approach2(input1, "teal"));
16+
assertTrue(anagrams.approach3(input1, "tale"));
17+
assertTrue(anagrams.approach3(input1, "teal"));
18+
assertTrue(anagrams.approach4(input1, "tale"));
19+
assertTrue(anagrams.approach4(input1, "teal"));
20+
}
21+
}

0 commit comments

Comments
 (0)