javascript [438。查找字符串中的所有字谜] #tags:leetcode

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript [438。查找字符串中的所有字谜] #tags:leetcode相关的知识,希望对你有一定的参考价值。

// Time Limit Exceeded

/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
var findAnagrams = function(s, p) {
  const result = [];
  for (let i = 0; i < (s.length - p.length + 1); ++i) {
    const substring = s.substring(i, i + p.length);
    if (isAnagram(substring, p)) {
      result.push(i);
    }
  }
  
  return result;
};

var isAnagram = function(s, t) {
    if (s.length !== t.length) {
      return false;
    }
    const map = {};
    let result = true;
  
    for (let i = 0; i < s.length; ++i) {
      const sc = s[i];
      const tc = t[i];
      
      if (map[sc]) {
        map[sc] += 1;
      } else {
        map[sc] = 1;
      }
      
      if (map[tc]) {
        map[tc] -= 1;
      } else {
        map[tc] = -1;
      }
    }
  
    _.each(map, (value, key) => {
      if (value !== 0) {
        result = false;
      }
    })
  
    return result;
  
};

/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */

const findAnagrams = function(s, p) {
    const result = [];
    const map = stringToHash(p);
    let i = 0;
    let curMap = {};
    
    for (let j = 0; j < s.length; ++j) {
    //   console.log('j=' + j)
      curMap = addToMap(curMap, s[j]);
      while((j - i + 1) > p.length) {
        // console.log('i='+i)
        curMap = removeFromMap(curMap, s[i]);
        i++;
      }

      if (i === 6) {
          console.log(j)
          console.log(curMap)
      }
      
      if ((j - i + 1) === p.length && isSameMap(curMap, map)) {
        result.push(i)
      }
    }
    
    return result;
  };
  
  const addToMap = (map, c) => {
    if (map[c]) {
      map[c] += 1;
    } else {
      map[c] = 1;
    }
    
    return map;
  }
  
  const removeFromMap = (map, c) => {
    if (map[c]) {
      map[c] -= 1;
      if (map[c] === 0) {
        delete map[c];
      }
    } else {
      map[c] = -1;
    }

    return map;
  }
  
  const isSameMap = (map1, map2) => {
    let result = true;
    _.map(map1, (value, key) => {
      if (map2[key] !== value) {
        result = false;
      }
    });

    _.map(map2, (value, key) => {
        if (map1[key] !== value) {
          result = false;
        }
      });

    return result;
  }
  
  const stringToHash = (s) => {
    const map = {};
    for (let i = 0; i < s.length; ++i) {
      if (map[s[i]]) {
        map[s[i]] += 1;
      } else {
        map[s[i]] = 1;
      }
    }
    
    return map;
  } 
// Time Limit Exceeded
/**
 * @param {string} s
 * @param {string} p
 * @return {number[]}
 */
var findAnagrams = function(s, p) {
  const result = [];
  for (let i = 0; i < (s.length - p.length + 1); ++i) {
    const substring = s.substring(i, i + p.length);
    if (isAnagram(substring, p)) {
      result.push(i);
    }
  }
  
  return result;
};

const sortString = (s) => {
  return s.split('').sort().join('');
};

const isAnagram = (s, p) => {
  if (sortString(s) === sortString(p)) {
    return true;
  }
  
  return false;
};

以上是关于javascript [438。查找字符串中的所有字谜] #tags:leetcode的主要内容,如果未能解决你的问题,请参考以下文章

438. Find All Anagrams in a String 438.查找字符串中的所有Anagrams

438. 找到字符串中所有字母异位词

438. 找到字符串中所有字母异位词

438. 找到字符串中所有字母异位词

438. 找到字符串中所有字母异位词

438. 找到字符串中所有字母异位词