Skip to content

Commit 22ddec2

Browse files
committed
Update Letter Count I
1 parent 9c163f1 commit 22ddec2

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed

Letter Count I

Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,40 +11,53 @@
1111
*
1212
* *
1313
* SOLUTION *
14+
* The first step is to remove all punctuation from the string being passed in which *
15+
* I do with the replace function. I also convert string to LowerCase to account for *
16+
* any words in the string that might be ProperCase. I then convert string into an *
17+
* array of words breaking on space. Next I loop through each word in the array. *
18+
* I loop through each character in the word and count the number of times the letter *
19+
* repeats. Then I compare the max times a letter is repeated in that word to the *
20+
* current value of maxCt which is initalized with a value of 1. If the number of *
21+
* repeated characters is greater then I update the maxCt with the new value and the *
22+
* current word is the maxWord. When finished loop I check to see if any word has *
23+
* repeated characters and if not return -1 else return the word with max repeated *
24+
* characters. *
1425
* *
1526
* Steps for solution *
16-
* 1) *
17-
* 2) *
18-
* 3) *
27+
* 1) Initialize variables *
28+
* 2) Convert string to lowerCase, remove all punctuation and store in array *
29+
* 3) Loop through each word in array and count the occurance of each letter *
30+
* 4) Compare occurance of repeated letter to maxCt *
31+
* 5) If greater update maxCt to new value and store word in maxWord *
32+
* 6) Return word with max repeated characters or -1 *
1933
* *
2034
***************************************************************************************/
2135

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

24-
var words = str.split(" ");
25-
var count = 0;
26-
var word = "";
27-
// code goes here
28-
for (var i = 0; i < words.length; i++) {
29-
var wordx = words[i];
30-
var sum = 0;
31-
for (var j = 0; j < words[i].length; j++) {
32-
var letter = wordx[j];
33-
for (var k = 0; k < wordx.length; k++) {
34-
if ((j != k) && (letter === wordx[k])){
35-
sum += 1;
40+
for(var i = 0; i < arr.length; i++){
41+
tempWord = arr[i];
42+
ctObj = {}
43+
44+
for(var j = 0; j <tempWord.length; j++){
45+
ctObj[tempWord[j]] = ctObj[tempWord[j]] || 0;
46+
ctObj[tempWord[j]]++;
47+
}
48+
for (var key in ctObj) {
49+
if (ctObj.hasOwnProperty(key)) {
50+
if (ctObj[key] > maxCt) {
51+
maxCt = ctObj[key];
52+
maxWord = tempWord;
53+
}
54+
}
3655
}
37-
}
3856
}
39-
if (sum > count) {
40-
count = sum;
41-
word = wordx;
57+
58+
if (maxCt === 1) {
59+
return -1;
60+
} else {
61+
return maxWord;
4262
}
43-
}
44-
if (count > 0) {
45-
return word;
46-
} else{
47-
return "-1";
48-
}
49-
5063
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy