Forked from AbdullahMagat/Hackerrank Java Anagrams Solution
Created
June 11, 2019 20:33
-
-
Save greatertomi/11e522d806536dc16497a8852e22d333 to your computer and use it in GitHub Desktop.
Hackerrank Java Anagrams Solution
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 equel. | |
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; | |
} | |
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" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment