MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / isAnagramUsingSorting

Method isAnagramUsingSorting

Java/valid-anagram.java:51–69  ·  view source on GitHub ↗
(String s, String t)

Source from the content-addressed store, hash-verified

49 // 2. Sorting approach
50 // Time Complexity: O(nlogn) | Space Complexity: O(n)
51 public boolean isAnagramUsingSorting(String s, String t) {
52
53 // length check
54 if (s.length() != t.length()){
55 return false;
56 }
57 else if(s.isEmpty()){ // empty string check
58 return true;
59 }
60
61 char [] ch1 = s.toCharArray();
62 char [] ch2 = t.toCharArray();
63
64 // sort both arrays
65 Arrays.sort(ch1);
66 Arrays.sort(ch2);
67
68 return Arrays.equals(ch1, ch2); // compare each character at same position or index
69 }
70
71 // 3. Efficient approach using hashing
72 // Time: O(n) | Space: O(1) [because the table's size stays constant no matter how large n is]

Callers

nothing calls this directly

Calls 2

lengthMethod · 0.80
isEmptyMethod · 0.45

Tested by

no test coverage detected