diff --git a/Java/Java Anagrams/Solution.java b/Java/Java Anagrams/Solution.java index 7317ce3..297e55c 100644 --- a/Java/Java Anagrams/Solution.java +++ b/Java/Java Anagrams/Solution.java @@ -29,91 +29,29 @@ // //Not Anagrams - - - - -import java.io.*; -import java.util.*; - public class Solution { - - //My hash map is initialized based on the fact that this is only using English characters as is stated in the problem constraints - //Overall time complexity: O(n) - //Overall space complexity: O(1) + static boolean isAnagram(String a, String b){ + a = a.toUpperCase(); + b = b.toUpperCase(); + char[] ar1 = a.toCharArray(); + java.util.Arrays.sort(ar1); + String sorted1 = String.valueOf(ar1); //sort the chars - static boolean isAnagram(String a, String b) - { - boolean anagram = true; - Character[] charSet = {'A','B','C','D','E', - 'F','G','H','I','J', - 'K','L','M','N','O', - 'P','Q','R','S','T', - 'U','V','W','X','Y','Z'}; - HashMap ALetterFrequency = new HashMap(); - HashMap BLetterFrequency = new HashMap(); - intializeHash(ALetterFrequency, charSet); //O(n) - intializeHash(BLetterFrequency, charSet); //O(n) - a = a.toUpperCase(); //O(n) - b = b.toUpperCase(); //O(n) - - //Originally I took a different approach to this problem because I was using substring and it - // runs in O(n) which would make my algorithm run in O(n^2) with constant space, but then I - // remembered that charAt() runs in constant time so utilizing that function I was able to - // have a time complexity of O(n) and space complexity of O(1) - - for(int i = 0;i