简体   繁体   English

如何在Javascript中有效地匹配和分组字符串?

[英]How to match and group strings efficiently in Javascript?

Write a function that returns an integer indicating number of times a group of string "pzmcb" appears in a string in no particualr orther. 编写一个函数,该函数返回一个整数,该整数指示一组字符串“ pzmcb”在字符串中没有出现的次数。 for example input string 1 -> "abdpsclmhz" output 1 -> 1 input string 2 : pzmcbcbpzmpcm output 2: 2 例如输入字符串1->“ abdpsclmhz”输出1-> 1输入字符串2:pzmcbcbpzmpcm输出2:2

I have written the code but it is not efficient and cannot handle large input string. 我已经编写了代码,但效率不高,无法处理较大的输入字符串。 I will appreciate it if an efficent way of writing this function can be provided 如果可以提供一种有效的编写此功能的方法,我将不胜感激

'use strict';

//pmzcbpmzcbpmz  [0 -4] [5 - 9] returns 2

function matchGroup(word) {
    let regex = /[pzmcb]/g
  let stringArray = word.match(regex);

  //console.log(stringArray);
  let cloneArray = [...stringArray];
  let stored = [];
  let searchString = "";
  let secondString = "";
  let temp = "";
    let tempArray = [];

  stringArray.forEach(item => {
    if (cloneArray.indexOf(item) >= 0 && searchString.indexOf(item) === -1) {
        searchString += item;
        if (searchString.length === 5) {
          stored.push(searchString);
          searchString = "";
        }
    } else if(secondString.indexOf(item) === -1){
        secondString += item;
      if (secondString.length === 5) {
          stored.push(searchString);
          secondString = "";
        }
    }else {
       temp += item;
      if (temp.length === 5) {
          tempArray.push(temp);
          temp = "";
      }
    }
});

    return stored.length;
// return integer
}


var paragraph = 'pzmcbpdfbcmz';
let result = matchGroup("abcdefpfklmhgzpzmcbpdfbcmzjklmoplzdsaklmcxheqgexcmpzdhgiwqertyhgdsbnmkjilopazxcsdertijuhgbdmlpoiqarstiguzcmnbgpoimhrwqasfgdhuetiopmngbczxsgreqad");
console.log(result);

I expect that the matchGroup function to return exact integers for large inputs 我希望matchGroup函数为大输入返回精确的整数

function countChar(char, string) {
    return (string.match(new RegExp(char, "g")) || []).length;
}

function countDistinctSubstring(sub, string) {
    firstChars = {};
    for (i = 0; i < sub.length; i++) {
        if (sub[i] in firstChars)
            firstChars[sub[i]]++;
        else
            firstChars[sub[i]] = 1;
    }
    return Math.min(...Object.keys(firstChars).map(key => Math.floor(countChar(key, string) / firstChars[key])));
}
> countDistinctSubstring("pzmcb", "abdpsclmhz");
< 1
> countDistinctSubstring("pzmcb", "pzmcbcbpzmpcm");
< 2
> countDistinctSubstring("pzmcbpdfbcmz", "abcdefpfklmhgzpzmcbpdfbcmzjklmoplzdsaklmcxheqgexcmpzdhgiwqertyhgdsbnmkjilopazxcsdertijuhgbdmlpoiqarstiguzcmnbgpoimhrwqasfgdhuetiopmngbczxsgreqad");
< 3

I can't tell for sure, but I think this is what you are looking for. 我不能肯定地说,但是我认为这就是您想要的。 It counts the number of occurrences of each letter in the small string, then finds the minimum ratio of occurrences in the large string to those in the small string for each character. 它计算小字符串中每个字母的出现次数,然后找到每个字符中大字符串与小字符串中出现的最小比率。 This minimum ratio is the maximum number of distinct times the small string can be composed of letters from the larger one. 此最小比率是小字符串可以由较大字符串中的字母组成的最大不重复次数。

Note that this answer was used in making the countChar function. 请注意, 此答案用于制作countChar函数。

I'd build up a map of character counts: 我会建立一个字符计数图:

   function countChars(str) {
     const count = {};
     for(const char of str) count[char] = (count[char] || 0) + 1;
     return count;
  }

Now we can easily build up count maps of the string to find and the source: 现在,我们可以轻松地建立要查找的字符串和源的计数映射:

   const toFind = countChars("pzmbc"),
      source = countChars("pzmcbpdfbcmz");

Now we can find the smallest relationship of chars to find and chars that are there: 现在,我们可以找到要查找的字符和存在的字符之间的最小关系:

  const result = Math.min(
     ...Object.entries(toFind).map(([char, need]) => Math.floor((source[char] || 0) / need))
  );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM