Valid Anagram
Problem
Input: s = "anagram", t = "nagaram" Output: trueInput: s = "rat", t = "car" Output: false
Pseudocode
Solution
Time and Space Complexity
Time
Space
Last updated
Input: s = "anagram", t = "nagaram" Output: trueInput: s = "rat", t = "car" Output: false
Last updated
anagrams consist of words that is of the same length and also same amount of identical characters. the sequence or order of letters are not important.
generaly intuition is to make a map of one string containing key-value pair of {letter: sumOfLetter}
traverse the other string while conduting some bookeeping.
return false if any letter is < 0
if the code reaches at the end of the loop, return true
or the shortcut method is to split both strings into array, sort according to alphabetical order and join them. then make a comparison if they are identical.// Some code
var isAnagram = function (s, t) {
if (s.length !== t.length) {
return false;
}
s = s.split("").sort().join();
t = t.split("").sort().join();
if (s === t) {
return true;
} else {
return false;
}
};