转圈圈

inPermutation

比较两个长度相同的字符串是否互为排列关系

 * @param {string} s1
 * @param {string} s2
 * @return {boolean}
 * note:
 * 1. s1,s2字符串的length相同
 */
function isPermutation(s1, s2) {
  const map = new Map();
  for (const i of s1) {
    map.set(i, map.get(i) + 1 || 1);
  }
  for (const i of s2) {
    if (!map.has(i)) return false;

    const count = map.get(i);
    if (count === 1) {
      map.delete(i);
    } else {
      map.set(i, count - 1);
    }
  }
  return map.size === 0;
}
posted @ 2019-05-22 17:35  rosendolu  阅读(117)  评论(0编辑  收藏  举报