-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJava anagrams.java
29 lines (26 loc) · 1.05 KB
/
Java 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
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
// Once you declare a.toUppercase you should assign it to a. you cannot define it as just a.toUppercase...
// I solved it with the long way however I could put a and b in a character array and then use Arrays.sort(arrayname). after this steps convert them to string and check if they are equal.
a=a.toUpperCase();
b=b.toUpperCase();
boolean ret = false;
StringBuilder c= new StringBuilder(b);
if(a.length()==b.length()){
for(int i=0; i<a.length();i++){
for(int j=0; j<c.length();j++){
if(a.charAt(i)==c.charAt(j)){
c.deleteCharAt(j);
if(i==a.length()-1 && c.length()==0){
ret=true;
break;
}
break;
}
}
}
}
return ret;
}
// Have a Amazing day.