Have the function LetterCount(str) take the str parameter being passed and return the first word with the greatest number of repeated letters.
For example: "Today, is the greatest day ever!" should return greatest because it has 2 e's (and 2 t's) and it comes before ever which also has 2 e's. If there are no words with repeating letters return -1. Words will be separated by spaces.
Letter count coderbyte java
import java.util.Arrays;
import java.util.Scanner;
public class LetterCount {
String LetterCount(String str) {
String[] words = str.split("\\s");
String winningWord = null;
int highestCharCount = 0;
for (String word : words) {
char[] chars = word.toCharArray();
Arrays.sort(chars);
char previousChar = chars[0];
int charCount = 0;
for (char c : chars) {
if (c == previousChar) {
charCount++;
} else {
if (charCount > highestCharCount) {
highestCharCount = charCount;
winningWord = word;
}
charCount = 1;
previousChar = c;
}
}
}
return highestCharCount == 1 ? "-1" : winningWord;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
LetterCount c = new LetterCount();
System.out.print(c.LetterCount(s.nextLine()));
}
}
Letter count coderbyte python
def LetterCount(str_):
#str = str.lower()
maxlt_word = None
max_lts = [(0, '')]
for word in str_.split():
if len(word) == len(set(word)):
continue
letter_list = [(word.count(letter), letter) for letter in set(word)]
max_of_lt = max(letter_list)[0]
if max_of_lt > 1:
word_max_lts = filter(
lambda x: x[0] == max(letter_list)[0], letter_list)
if max(word_max_lts)[0] > max(max_lts)[0]:
maxlt_word = word
max_lts = word_max_lts
if max(word_max_lts)[0] == max(max_lts)[0]:
if len(word_max_lts) > len(max_lts):
maxlt_word = word
max_lts = word_max_lts
return maxlt_word or -1
# keep this function call here
# to see how to enter arguments in Python scroll down
print LetterCount(raw_input())