返回重复字母最多的第一个单词
Return the first word with the greatest number of repeated letters
这是一个来自coderbyte的简单集的问题。很多人已经问过这个问题了,但我真的很好奇我的特殊解决方案出了什么问题(我知道这是一个非常愚蠢和低效的…)
原始问题:让函数LetterCountI(str)接受传入的str形参,并返回重复字母最多的第一个单词。例如:"今天是最伟大的一天!"应该返回"最伟大的一天",因为它有两个e(和两个t),而且它出现在同样有两个e的任何一天之前。如果没有包含重复字母的单词,返回-1。单词之间用空格分隔。
我的解决方案大多数时候都有效。但是如果输入的最后一个单词似乎不受我的代码的重视。例如,对于" a bb ccc ",将返回" bb "而不是" ccc "。但有趣的是,如果字符串只包含一个单词,结果是正确的。例如," ccc "返回" ccc "。
请告诉我哪里错了。提前感谢!function LetterCountI(str) {
str.toLowerCase();
var arr = str.split(" ");
var count = 0;
var word = "-1";
for (var i = 0; i < arr.length; i++) {
for (var a = 0; a < arr[i].length; a++) {
var countNew = 0;
for (var b = a + 1; b < arr[i].length; b++) {
if(arr[i][a] === arr[i][b])
countNew += 1;
}
if (countNew > count) {
count = countNew;
word = arr[i];
}
}
return word;
}
}
请在下面找到您的代码的可行版本:
function LetterCountI(str) {
str = str.toLowerCase();
var arr = str.split(" ");
var count = 0;
var word = "-1";
for (var i = 0; i < arr.length; i++) {
for (var a = 0; a < arr[i].length; a++) {
var countNew = 0;
for (var b = a + 1; b < arr[i].length; b++) {
if (arr[i][a] === arr[i][b])
countNew += 1;
}
if (countNew > count) {
count = countNew;
word = arr[i];
}
}
}
return word;
}
您需要将return word;
语句移出循环以修复您的版本。
我还把另一种算法放在一起,它依赖于一些内置的javascript方法,如Array.map
和Math.max
,仅供参考。我做了几次测试,似乎快了几毫秒,但也没快多少。
function LetterCountI(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;
}
});
//covert 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;
}
这是解决您的问题的Java代码
你的答案不正确。您应该在"for循环"中返回word/Answer/res。查看我的代码。
public static String StringChallenge( String str) {
String[] arr = str.split(" ");
int count = 0; String res = "-1";
for (int i = 0; i < arr.length ; i++) {
for (int j = 0; j < arr[i].length() ; j++) {
int counter = 0;
for (int k = j + 1; k < arr[i].length() ; k++) {
if(arr[i].charAt(j) === arr[i].charAt(k) )
counter ++;
}
if (counter > count) {
count = counter; res = arr[i];
}
}
return res;
}
}
我认为问题是您将return
语句放在最外层循环内。
所以你必须把return
语句放在内循环中。
正确使用return
if (countNew > count) {
count = countNew;
word = arr[i];
}
return word;
}
}
}
另一个更函数式编程风格的解决方案:
JavaScriptfunction LetterCountI(str) {
return ((str = str.split(' ').map(function(word) {
var letters = word.split('').reduce(function(map, letter) {
map[letter] = map.hasOwnProperty(letter) ? map[letter] + 1 : 1;
return map;
}, {}); // map of letters to number of occurrences in the word
return {
word: word,
count: Object.keys(letters).filter(function(letter) {
return letters[letter] > 1;
}).length // number of repeated letters
};
}).sort(function(a, b) { // Sort words by number of repeated letters
return b.count - a.count;
}).shift()) && str.count && str.word) || -1; // return first word with maximum repeated letters or -1
}
console.log(LetterCountI('Today, is the greatest day ever!')); // => greatest
恰好
http://plnkr.co/edit/BRywasUkQ3KYdhRpBfU2?p =预览
我建议使用正则表达式:/a+/g
来查找包含关键字a
的字母列表。
我的例子:
var str = aa yyyyy bb cccc cc dd bbb;
首先,找到一个不同的单词列表:
>>> ["a", "y", "b", "c", "d"]
对不同单词列表中的每个单词使用正则表达式:
var word = lstDiffWord[1];
var
wordcount = str.match(new RegExp(word+'+','g'));
console.log(wordcount);
>>>>["yyyyy"]
下面是完整的示例:http://jsfiddle.net/sxro0sLq/4/
- 字母计数:返回重复字母数最多的第一个单词
- 我如何从字符串中选出第一个单词的第一个字母,然后再选出第二个单词
- 查找表单输入的第一个单词
- JavaScript只接收句子中的第一个单词
- 突出显示自动完成输入框中的第一个单词
- 我怎样才能只从带有 javascript 的字符串中获取第一个单词
- Javascript从2个单词短语中删除第二个单词,而无需更改第一个单词
- 除句子的第一个单词之外的每个单词的正则表达式
- 获取字符串的第一个单词
- 如何锁定文本区域的第一个单词
- 在CodeMirror中查找自定义语言模式的段落的第一个单词和最后一个单词
- CoffeeScript:捕获字符串中的第一个单词
- 获取java脚本websockets中的第一个单词
- 检查文本区域(PHP)中的第一个单词
- 如何获取字符串中的第一个单词
- 获取第一个单词和换行符之间的内容
- 选择容器中的第一个单词
- 第一个单词的正则表达式是星期几
- 如何获取字符串的第一个单词并将其转换为int?jQuery
- 查找浏览器将分类为溢出的第一个单词