#44 同字母异序


  • 0

    哇!感觉自己好笨

    const isAnagram = (str1, str2) => {
      if(str1.length != str2.length){
        return false
      }
      var resultObj = {};
      var strOneArr = str1.split('');
      var strTwoArr = str2.split('');
      for(let i = 0; i<strOneArr.length;i++){
        //存入第一个
        if(resultObj[strOneArr[i]]){
          ++resultObj[strOneArr[i]];
        }else{
          resultObj[strOneArr[i]] = 1;
        }
        //取出第二个
        if(resultObj[strTwoArr[i]]){
          --resultObj[strTwoArr[i]];
        }else{
          resultObj[strTwoArr[i]] = -1;
        }
      }
      let resultObjArr = Object.keys(resultObj);
      for(let i = 0; i<resultObjArr.length;i++){
        if(resultObj[resultObjArr[i]] != 0){
          return false;
        }
      }
      return true
    }
    

  • 0

    @nuxio 厉害了


  • 0

    const isAnagram = (str1, str2) => {
    return (str1.split('').sort().join('')===str2.split('').sort().join(''))
    }
    佛系心态,见怪不怪!


  • 0

    const isAnagram = function(str1, str2){
      if(typeof str1 !== 'string' || typeof str2 !== 'string'){
        return false
      }
      let result = true;
      str1 = str1.split('')
      str2 = str2.split('')
      str1.forEach(function(s1){if(!str2.includes(s1)){result = false;}})
      str2.forEach(function(s2){if(!str1.includes(s2)){result = false;}})
      if(str1.length !== str2.length){
        result = false
      }
      return result;  
    }
    

  • 0

    只需要三行判断

    const isAnagram = (str1, str2) =>{
        let str1s = str1.split('').sort().join('');
        let str2s = str2.split('').sort().join('');
        return str1s===str2s
    } /* TODO */
    
    

  • 0

    @nuxio 这个简洁


  • 0

    @nikaple 赞!


  • 0

    const isAnagram = (str1, str2) => {
    
       str1 = str1.toUpperCase();
       str2 = str2.toUpperCase();
    
       if(str1 === str2 || str1.length !== str2.length){
           return false;
       }
    
       function fn(str) {
           return str.split("").sort(function (x, y) {
               return x > y ? 1 : x < y ? -1 : 0;
           });
       }
    
       if (fn(str1).join(",") === fn(str2).join(",")) {
           return true;
       } else {
           return false;
       }
    };
    

  • 0

    const isAnagram = (str1, str2) => {
    str1 = [...str1].sort().join('');
    str2 = [...str2].sort().join('');
    return str1 === str2;
    }


  • 0

    const isAnagram = (str1, str2) => str1.split('').sort().join('') == str2.split('').sort().join('')
    

  • 0

    // 方法一
    const isAnagram = (str1, str2) => {
      if(str1.length !== str2.length){
        return false;
      }
      let result = (str1 || '').split('').every((el) => {
        return str2.indexOf(el) > -1;
      });
      return result;
    }
    // 方法二
    const isAnagram = (str1, str2) => {
      let str1res = str1.split('').sort().join('');
      let str2res =str2.split('').sort().join('');
      return str1res === str2res;
    }
    

  • 0

    const isAnagram = (str1, str2) => str1.split( '' ).sort().join('') === str2.split( '' ).sort().join('')

    关键是发现一个规律,符合“相同字母”“顺序不同”的字符串,经过排序sort后是可以转化成一模一样的2个字符串的,直接对比2个字符串是否全等即可


  • 0

    const isAnagram = (str1, str2) => {
    if(str1.length !== str2.length){
    return false;
    }
    let arr1 = [...str1];
    let arr2 = [...str2];
    return arr1.sort().toString() === arr2.sort().toString();
    }


  • 0

    const isAnagram = (str1, str2) => {
    return str1.split('').sort().join('') == str2.split('').sort().join('');
    }


  • 0

    @nikaple 厉害


  • 0

    const isAnagram = (str1, str2) => {
    
    	let arr1 = str1.split('');
    	let arr2 = str2.split('');
    	let a =  arr1.every((item) => arr2.includes(item))
    	let b =  arr2.every((item) => arr1.includes(item))
    	return a && b && (arr1.length == arr2.length);
    }
    

  • 0

    统计每个字母的数量然后比较相等即可。或者偷懒一下,用sort


  • 0

    const isAnagram = (str1, str2) =>
    [...str1].sort().toString()===[...str2].sort().toString()


  • 0

    const isAnagram = (str1, str2) => {
      function toObj (str) {
        let O = {}
        Array.prototype.slice.call(str).forEach(v => {
          O[v] ? (O[v] = ++O[v]) : (O[v] = 1)
        })
        return O
      }
      let a = toObj(str1)
      let b = toObj(str2)
      let _a = Object.keys(a).length
      let _b = Object.keys(b).length
      if (_a === _b) {
        let index = 0
        for (let k in a) {
          if (a[k] !== b[k]) {
            break
          } else {
            index++
            if (index === _a) {
              return true
            }
          }
        }
      }
      return false
    }
    

  • 0

    const isAnagram = (str1, str2) => {
        for(let s of str1){
            const index = str2.indexOf(s) 
            if(index === -1)return false;
            str2 = str2.slice(0, index) + str2.slice(index + 1);
        }
        return str2 === '';
    }
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待