-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
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
✅242. 有效的字母异位词 #44
Comments
解题思路:
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function (s, t) {
if (s.length !== t.length) return false;
let map = new Map();
for (let i = 0; i < s.length; i++) {
let k = s[i],
j = t[i];
if (map.has(k)) {
map.set(k, map.get(k) + 1);
} else {
map.set(k, 1);
}
if (map.has(j)) {
map.set(j, map.get(j) - 1);
} else {
map.set(j, -1);
}
}
for (const key of map) {
if (key[1] !== 0) return false;
}
return true;
}; |
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if(s.length != t.length) return false;
return s.split('').sort().join('') == t.split('').sort().join('');
} |
解法一 哈希表 /**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if (s.length != t.length) return false;
const map = new Map();
for (let i = 0; i < s.length; i++) {
const x = s[i];
const y = t[i];
if (map.has(x)) {
map.set(x, map.get(x) + 1);
} else {
map.set(x, 1)
}
if (map.has(y)) {
map.set(y, map.get(y) - 1);
} else {
map.set(y, -1)
}
}
for (let [key, value] of map) {
if (value !== 0) return false;
}
return true;
}; 解法二 排序合并 var isAnagram = function(s, t) {
if (s.length != t.length) return false;
return s.split('').sort().join('') === t.split('').sort().join('')
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
242. 有效的字母异位词
给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
示例 1:
示例 2:
进阶:
如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?
The text was updated successfully, but these errors were encountered: