-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAnagrams.java
305 lines (248 loc) · 7.06 KB
/
Anagrams.java
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/*
Two strings, a and b
, are called anagrams if they contain all the same characters in the same frequencies. For this challenge, the test is not case-sensitive. For example, the anagrams of CAT are CAT, ACT, tac, TCA, aTC, and CtA.
Function Description
Complete the isAnagram function in the editor.
isAnagram has the following parameters:
string a: the first string
string b: the second string
Returns
boolean: If a
and b
are case-insensitive anagrams, return true. Otherwise, return false.
Input Format
The first line contains a string a
.
The second line contains a string b
.
Constraints
Strings a and b
consist of English alphabetic characters.
The comparison should NOT be case sensitive.
Sample Input 0
anagram
margana
Sample Output 0
Anagrams
Explanation 0
Character Frequency: anagram Frequency: margana
A or a 3 3
G or g 1 1
N or n 1 1
M or m 1 1
R or r 1 1
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
Sample Input 1
anagramm
marganaa
Sample Output 1
Not Anagrams
Explanation 1
Character Frequency: anagramm Frequency: marganaa
A or a 3 4
G or g 1 1
N or n 1 1
M or m 2 1
R or r 1 1
The two strings don't contain the same number of a's and m's, so we print "Not Anagrams".
Sample Input 2
Hello
hello
Sample Output 2
Anagrams
Explanation 2
Character Frequency: Hello Frequency: hello
E or e 1 1
H or h 1 1
L or l 2 2
O or o 1 1
The two strings contain all the same letters in the same frequencies, so we print "Anagrams".
*/
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
/* Complete the function
find the char at each pos and count it in string A
do the same thing in String B and compare the each char with B
char first_alphabet_a=a.charAt(0);
char frist_alphabet_b=b.charAt(0);
if (a.length()!=b.length()){
System.out.println("Not Anagrams");
}
else{
for (int i=0;i<a.length()-1;i++){
char value_char
}
}
return true;
*/
char a_string[]=a.toLowerCase().toCharArray();
char b_string[]=b.toLowerCase().toCharArray();
boolean returnValue;
if(a_string !=null && b_string!=null){
java.util.Arrays.sort(a_string);
java.util.Arrays.sort(b_string);
returnValue=java.util.Arrays.equals(a_string,b_string);
return returnValue;
}
else{
return returnValue=false;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );
}
}
/*
Another Mehthod -2
//Enter code here
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String word=sc.nextLine();
String phrase=sc.nextLine();
String result="";
String result1="";
//boolean value=false;
for(int i=0; i<word.length(); i++){
if(word.charAt(i) !=' '){
result=result+word.charAt(i);
}
}
for(int i=0; i<phrase.length(); i++){
if(phrase.charAt(i) !=' '){
result1=result1+phrase.charAt(i);
}
}
char[] first_word=new char[result.length()];
char[] second_word=new char[result1.length()];
for(int i=0; i<result.length(); i++){
first_word[i]=result.charAt(i);
}
for(int j=0; j<second_word.length; j++){
second_word[j]=result1.charAt(j);
}
//Arrays.sort(first_word);
// Arrays.sort(second_word);
boolean value=true;
for(int i=0; i<first_word.length; i++){
for(int j=0; j<second_word.length; j++){
if(first_word.charAt(i) != second_word.charAt(i)){
value=false;
}
}
}
//System.out.println(result+" " +result1);
String name= Arrays.equals(first_word,second_word) ? "True" : "False";
System.out.println(name);
}
}
*/
/*
Anothear Method
//Enter code here
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String word=sc.nextLine();
String phrase=sc.nextLine();
String result="";
String result1="";
//boolean value=false;
for(int i=0; i<word.length(); i++){
if(word.charAt(i) !=' '){
result=result+word.charAt(i);
}
}
for(int i=0; i<phrase.length(); i++){
if(phrase.charAt(i) !=' '){
result1=result1+phrase.charAt(i);
}
}
char[] first_word=new char[result.length()];
char[] second_word=new char[result1.length()];
for(int i=0; i<result.length(); i++){
first_word[i]=result.charAt(i);
}
for(int j=0; j<second_word.length; j++){
second_word[j]=result1.charAt(j);
}
Arrays.sort(first_word);
Arrays.sort(second_word);
//System.out.println(result+" " +result1);
String name= Arrays.equals(first_word,second_word) ? "True" : "False";
System.out.println(name);
}
}
*/
/* Another Method
//Enter code here
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String word=sc.nextLine();
String phrase=sc.nextLine();
String result="";
String result1="";
//boolean value=false;
for(int i=0; i<word.length(); i++){
if(word.charAt(i) !=' '){
result=result+word.charAt(i);
}
}
for(int i=0; i<phrase.length(); i++){
if(phrase.charAt(i) !=' '){
result1=result1+phrase.charAt(i);
}
}
char[] first_word=new char[result.length()];
char[] second_word=new char[result1.length()];
for(int i=0; i<result.length(); i++){
first_word[i]=result.charAt(i);
}
for(int j=0; j<second_word.length; j++){
second_word[j]=result1.charAt(j);
}
//Arrays.sort(first_word);
// Arrays.sort(second_word);
char temp;
for(int i=0; i<first_word.length; i++){
for(int j=i+1; j<first_word.length; j++){
if(first_word[i]>first_word[j]){
temp=first_word[i];
first_word[i]=first_word[j];
first_word[j]=temp;
}
}
}
char temp1;
for(int i=0; i<second_word.length; i++){
for(int j=i+1; j<second_word.length; j++){
if(second_word[i]>second_word[j]){
temp1=second_word[i];
second_word[i]=second_word[j];
second_word[j]=temp1;
}
}
}
//System.out.println(second_word);
for(char value: second_word){
// System.out.println(value);
}
//System.out.println(result+" " +result1);
String name= Arrays.equals(first_word,second_word) ? "True" : "False";
System.out.println(name);
}
}
*/