Letter Count I Coderbyte Solution

0

Have the function LetterCountI(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 I coderbyte Java


public class LetterCountOne {
String LetterCountI(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) {

Scanner s = new Scanner(System.in);
LetterCountOne c = new LetterCountOne();
System.out.print(c.LetterCountI(s.nextLine()));
}
}

Letter Count I coderbyte Python


def LetterCountI(str):
greatest = "-1"
greatestCount = 0;

def findRepeats(word):
storage = {}
rep = 0
for letter in list(word):
try:
storage[letter] = storage[letter] + 1
except:
storage[letter] = 1
for n in storage:
if storage[n] > 1:
rep = rep + storage[n]
return rep

arr = str.split()

for word in arr:
c = findRepeats(word)
if c > greatestCount:
greatest = word
greatestCount = c

return

Letter Count I JavaScript


function LetterCountI(str) {
var ctObj, tempWord, maxWord, maxCt = 1;
var arr = str.toLowerCase().replace(/[^a-zA-Z ]/g,"").split(" ");

for(var i = 0; i < arr.length; i++){
tempWord = arr[i];
ctObj = {}

for(var j = 0; j <tempWord.length; j++){
ctObj[tempWord[j]] = ctObj[tempWord[j]] || 0;
ctObj[tempWord[j]]++;
}
for (var key in ctObj) {
if (ctObj.hasOwnProperty(key)) {
if (ctObj[key] > maxCt) {
maxCt = ctObj[key];
maxWord = tempWord;
}
}
}
}
if (maxCt === 1) {
return -1;
} else {
return maxWord;
}
}

Explanation

The first step is to remove all punctuation from the string being passed in which I do with the replace function. I also convert string to LowerCase to account for any words in the string that might be ProperCase. I then convert string into an array of words breaking on space. Next I loop through each word in the array. I loop through each character in the word and count the number of times the letter repeats. Then I compare the max times a letter is repeated in that word to the current value of maxCt which is initalized with a value of 1. If the number of repeated characters is greater then I update the maxCt with the new value and the current word is the maxWord.  When finished loop I check to see if any word has repeated characters and if not return -1 else return the word with max repeated characters. 

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !