You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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
+
importjava.util.Arrays;
4
+
importjava.util.HashMap;
8
5
9
-
packagecom.thealgorithms.strings;
10
-
importjava.util.*;
11
-
publicclassAnagrams
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
+
publicclassAnagrams {
13
15
// 4 approaches are provided for anagram checking. approach 2 and approach 3 are similar but differ in running time.
14
-
publicstaticvoidmain(Stringargs[]) {
16
+
publicstaticvoidmain(String[] args) {
15
17
Stringfirst = "deal";
16
18
Stringsecond = "lead";
17
19
// All the below methods takes input but doesn't return any output to the main method.
18
-
Anagramsnm=newAnagrams();
20
+
Anagramsnm = newAnagrams();
19
21
System.out.println(nm.approach2(first, second)); /* To activate methods for different approaches*/
20
22
System.out.println(nm.approach1(first, second)); /* To activate methods for different approaches*/
21
23
System.out.println(nm.approach3(first, second)); /* To activate methods for different approaches*/
@@ -37,114 +39,88 @@ public static void main(String args[]) {
37
39
*/
38
40
}
39
41
40
-
booleanapproach1(Strings, Stringt)
41
-
{
42
-
if (s.length() != t.length())
43
-
{
42
+
booleanapproach1(Strings, Stringt) {
43
+
if (s.length() != t.length()) {
44
44
returnfalse;
45
-
}
46
-
else
47
-
{
45
+
} else {
48
46
charc[] = s.toCharArray();
49
47
chard[] = t.toCharArray();
50
48
Arrays.sort(c);
51
49
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)) {
54
51
returntrue;
55
-
} else
56
-
{
52
+
} else {
57
53
returnfalse;
58
54
}
59
55
}
60
56
}
61
57
62
-
booleanapproach2(Stringa, Stringb)
63
-
{
64
-
if(a.length()!=b.length())
65
-
{
58
+
booleanapproach2(Stringa, Stringb) {
59
+
if (a.length() != b.length()) {
66
60
returnfalse;
67
-
}
68
-
else
69
-
{
70
-
intm[]=newint[26];
71
-
intn[]=newint[26];
72
-
for(charc: a.toCharArray())
73
-
{
74
-
m[c-'a']++;
61
+
} else {
62
+
intm[] = newint[26];
63
+
intn[] = newint[26];
64
+
for (charc : a.toCharArray()) {
65
+
m[c - 'a']++;
75
66
}
76
67
// 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
68
// Running time and space complexity of this algo is less as compared to others
78
-
for(charc:b.toCharArray())
79
-
{
80
-
n[c-'a']++;
69
+
for (charc : b.toCharArray()) {
70
+
n[c - 'a']++;
81
71
}
82
-
for(inti=0;i<26;i++)
83
-
{
84
-
if(m[i]!=n[i])
85
-
{
72
+
for (inti = 0; i < 26; i++) {
73
+
if (m[i] != n[i]) {
86
74
returnfalse;
87
75
}
88
76
}
89
77
returntrue;
90
78
}
91
79
}
92
80
93
-
booleanapproach3(Strings, Stringt)
94
-
{
95
-
if(s.length()!=t.length())
96
-
{
81
+
booleanapproach3(Strings, Stringt) {
82
+
if (s.length() != t.length()) {
97
83
returnfalse;
98
84
}
99
85
// this is similar to approach number 2 but here the string is not converted to character array
100
-
else
101
-
{
102
-
inta[]=newint[26];
103
-
intb[]=newint[26];
104
-
intk=s.length();
105
-
for(inti=0;i<k;i++)
106
-
{
107
-
a[s.charAt(i)-'a']++;
108
-
b[t.charAt(i)-'a']++;
86
+
else {
87
+
inta[] = newint[26];
88
+
intb[] = newint[26];
89
+
intk = s.length();
90
+
for (inti = 0; i < k; i++) {
91
+
a[s.charAt(i) - 'a']++;
92
+
b[t.charAt(i) - 'a']++;
109
93
}
110
-
for(inti=0;i<26;i++)
111
-
{
112
-
if(a[i]!=b[i])
94
+
for (inti = 0; i < 26; i++) {
95
+
if (a[i] != b[i])
113
96
returnfalse;
114
97
}
115
98
returntrue;
116
99
}
117
100
}
118
101
119
-
booleanapproach4(Strings, Stringt)
120
-
{
121
-
if(s.length()!=t.length())
122
-
{
123
-
returnfalse;
102
+
booleanapproach4(Strings, Stringt) {
103
+
if (s.length() != t.length()) {
104
+
returnfalse;
124
105
}
125
106
// 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
0 commit comments