Skip to content

Commit

Permalink
Merge pull request #2555 from itshagunrathore/validanagramsorting
Browse files Browse the repository at this point in the history
Fixed anagram hashing and added sorting
  • Loading branch information
fineanmol authored Oct 5, 2022
2 parents 562f26f + 32a4132 commit b7294ef
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,17 @@ public static boolean isAnnagramUsingFrequencyCounter(String s, String t) {
// You can use py counters -- return Counter(s) == Counter(t)

}

public static boolean isAnagramUsingSorting(String s, String t) {
if (s.length() != t.length()) return false;
// You can use py counters -- return Counter(s) == Counter(t)
// You can also use sroting -- sorted(s) == sorted(t) -- PY || O(nlogn) /O(1)
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
Arrays.sort(str1);
Arrays.sort(str2);
return Arrays.equals(str1, str2);
}

public static boolean isAnagramIUsingHash(String s, String t) {

if (s.length() != t.length()) return false;
Expand Down

0 comments on commit b7294ef

Please sign in to comment.