We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
原题链接:https://leetcode-cn.com/problems/valid-anagram/
解题思路:
/** * @param {string} s * @param {string} t * @return {boolean} */ var isAnagram = function (s, t) { // 两个字符串的长度不相等,其中的各个字母数量必然不相等 if (s.length !== t.length) { return false; } // 使用Map保存各个字母的数量 let map = new Map(); for (let i = 0; i < s.length; i++) { const sChar = s[i]; // 字符串s中的字母 const tChar = t[i]; // 字符串t中的字母 // 遍历s中的每个字母时,将Map中的数量+1 typeof map.get(sChar) === 'number' ? map.set(sChar, map.get(sChar) + 1) : map.set(sChar, 1); // 设置map的初始值 // 遍历t中的每个字母时,将Map中的数量-1 // 完成遍历后,如果两个字符串中的字母数量相等,则Map中保存的值为0 typeof map.get(tChar) === 'number' ? map.set(tChar, map.get(tChar) - 1) : map.set(tChar, -1); // 设置map的初始值 } // 遍历Map中存储的每个字母数量 for (const [name, number] of map) { // 如果有数量不为0,表示两个字符串的字母数量不相等 if (number > 0) { return false; } } // 如果正常退出for...of循环,表示两个字符串的字母数量相等 return true; };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
原题链接:https://leetcode-cn.com/problems/valid-anagram/
解题思路:
The text was updated successfully, but these errors were encountered: