【问题标题】:Return greatest number of repeated characters in a sentence返回句子中最大重复字符数
【发布时间】:2016-02-12 03:35:56
【问题描述】:

好吧,也许我最后一个问题还不够清楚。

我想返回字符串中重复字符最多的单词。

所以下面的字符串:

There/'s a passage that I got memorized, seems appropriate for this situation: Ezekiel 25,17.

将返回["appropriate"]

但如果有多个单词具有相同数量的重复字符,我想将它们全部归还。所以下面的字符串

Hello all from Boston

将返回["Hello", "all", "boston"]

这是我到目前为止的代码。此代码取自另一个 stackoverflow thread

function returnRepeatChar(str){
  var maxCount = 0;
  var word = '-1';
  
  //split string into words based on spaces and count repeated characters
  str.toLowerCase().split(" ").forEach(function(currentWord){
    var hash = {};
    
    //split word into characters and increment a hash map for repeated values
    currentWord.split('').forEach(function(letter){
      if (hash.hasOwnProperty(letter)){
        hash[letter]++;
      } else {
        hash[letter] = 1;
      }
    });
    
    //convert the hash map to an array of character counts
    var characterCounts = Object.keys(hash).map(function(key){
      return hash[key];
    });
    
    //find the maximum value in the squashed array
    var currentMaxRepeatedCount = Math.max.apply(null, characterCounts);
    
    //if the current word has a higher repeat count than previous max, replace it 
    if (currentMaxRepeatedCount > maxCount){
      maxCount = currentMaxRepeatedCount;
      word = currentWord;
    }
    
  });
  return word;
}

console.log(returnRepeatChar("There/'s a passage that I got memorized, seems appropiate for this situation: Ezekiel 25,17.")); //"appropriate"

【问题讨论】:

  • 对于“克里斯的脾脏”,结果是[Chris's, spleen] 还是[spleen]? “男人的狗”是[man's, dog] 还是[man, dog]
  • word 从字符串更改为数组。如果当前计数大于最大计数,则将word 设置为包含当前单词的数组。如果当前计数与最大计数相同,则将当前单词压入数组。否则,它与您拥有的完全相同。
  • 如果你忽略了将单词标记为单词的难度,那么问题可以分解为小而简单的步骤。构建一个包含每个标记的“分数”的数组,跟踪最大分数是多少。然后取所有得分最高的单词。一个单词的评分函数非常简单……它只是一个循环。
  • 我认为我不必担心 ' ,但是在你的场景中,[Chris's, spleen] 和 man's dog 没有重复的单词,所以它会返回 -1...
  • 通过询问“man's dog”,我试图询问这个词是“man's”还是“man”。我认为你的第一个答案回答了这个问题。谢谢。

标签: javascript regex


【解决方案1】:

对您的代码进行以下简单更改。

var word = "-1";

变成:

var word = [];

更新word的代码变成:

//if the current word has a higher repeat count than previous max, replace it 
if (currentMaxRepeatedCount > maxCount){
  maxCount = currentMaxRepeatedCount;
  word = [currentWord];
} else if (currentMaxRepeatedCount == maxCount) {
  // If it's the same as the max, add it to the list
  word.push(currentWord);
}

【讨论】:

    【解决方案2】:

    是的,你可以做到。尝试以下脚本。

    function largest(arr){
            var sortArr = arr.sort(function(a,b){return b.length - a.length});
            return sortArr;
        }
    
        function returnRepeatChar(str){
           return largest(str.toLowerCase().split(" "));
        }
        //This function return the sorted of the given string
        var sorted = returnRepeatChar("There\'s a passage that I got memorized, seems appropriate for this situation: Ezekiel 25,17.");
    alert(sorted);
    

    【讨论】:

    • 你的largest函数在哪里计算单词中重复字符的数量?看起来您只是在寻找句子中最长的单词。
    • 你只返回一个单词,而不是所有重复次数最多的单词。
    • 这个问题与他链接到的上一个问题之间的全部区别在于,另一个问题只找到一个单词,他想要所有单词。你似乎完全忽略了这个关键细节。
    • @Barmar,好的,谢谢。下次活动前我必须更加小心。
    • 我不认为return console.log(... 会做你想做的事。
    猜你喜欢
    • 2017-03-30
    • 2016-08-24
    • 1970-01-01
    • 2020-04-15
    • 2021-10-22
    • 2018-02-17
    • 2020-02-01
    • 2015-09-17
    • 1970-01-01
    相关资源
    最近更新 更多