LeetCode Js-242. Valid Anagram
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
給予兩個字串分別為 s 和 t,如果 t 是 s 的重組字,則回傳 true,否則回傳 false。
一個重組字是透過重新排列得到不同的單字或片語,通常只使用所有原始字母一次。
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Solution:
Code:
var isAnagram = function(s, t) {
if (s.length !== t.length) return false
var s = s.split("").sort().join("")
var t = t.split("").sort().join("")
return s === t
};
FlowChart:
Example 1
Input: s = "anagram", t = "nagaram"
s = "aaagmnr"
t = "aaagmnr"
return true