Given two strings s and t, write a function to determine if t is an anagram of s.javascript
For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.java
思路:將兩個字符串都轉換爲含有每一個元素的數組,再將兩個數組排序,若是兩個數組的每一個元素都對應相同,則返回true,不然返回false。
數組
language :javascriptspa
/** * @param {string} s * @param {string} t * @return {boolean} */ var isAnagram = function(s, t) { var sArray = s.split('').sort(); var tArray = t.split('').sort(); if(sArray.length != tArray.length){ return false; } var flag = true; for(var i=0;i<sArray.length;i++){ if(sArray[i] != tArray[i]){ flag = false; break; } } return (flag? true : false); };