作者:力扣 (LeetCode) 链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xn96us/ 来源:力扣(LeetCode) 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

题目

给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。

示例 1:

输入: s = “anagram”, t = “nagaram”

输出: true

示例 2:

输入: s = “rat”, t = “car”

输出: false

说明:

你可以假设字符串只包含小写字母。

相关标签
排序 哈希表

题解

  1. /**
  2. * @param {string} s
  3. * @param {string} t
  4. * @return {boolean}
  5. */
  6. var isAnagram = function(s, t) {
  7. if (s.length !== t.length) {
  8. return false;
  9. }
  10. let _s = s.split('').sort().join('');
  11. let _t = t.split('').sort().join('');
  12. if (_s !== _t) {
  13. return false;
  14. }
  15. return true;
  16. };

参考:
https://leetcode-cn.com/problems/valid-anagram/solution/you-xiao-de-zi-mu-yi-wei-ci-by-leetcode-solution/