Skip to content

Latest commit

 

History

History
47 lines (26 loc) · 1.65 KB

File metadata and controls

47 lines (26 loc) · 1.65 KB

Approach

This code snippet contains a function that checks if two given strings are anagrams of each other. Here's how it works:

1-First, if the lengths of the two strings are different, meaning they are not of the same length, the function returns false.

2-Then, both strings are converted into arrays of characters using the split('') method, and each array is sorted alphabetically using the sort() method. This gives us the sorted versions of both strings.

3-Next, a loop is initiated for each element in the sorted_T array, and each element is compared with the corresponding element in the sorted_S array.

4-If any element doesn't match during the comparison (meaning the characters at the respective indexes in the sorted arrays are different), the function returns false.

5-If all elements match until the end of the loop (meaning both strings have the same characters), the function returns true.

Complexity

  • Time complexity:O(n log n)
  • Space complexity:O(1)

Code

var isAnagram = function(s, t) {

    if (s.length !== t.length) return false;
    
    const sorted_S = s.split('').sort();
    const sorted_T = t.split('').sort();
    
    for (let i = 0; i < sorted_T.length; i++) {
      
      if (sorted_S[i] !== sorted_T[i]) return false; 
        
    }
    
    return true;
  
    
};