|
11 | 11 | *
|
12 | 12 | * *
|
13 | 13 | * 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. * |
14 | 25 | * *
|
15 | 26 | * 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 * |
19 | 33 | * *
|
20 | 34 | ***************************************************************************************/
|
21 | 35 |
|
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(" "); |
23 | 39 |
|
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 | + } |
36 | 55 | }
|
37 |
| - } |
38 | 56 | }
|
39 |
| - if (sum > count) { |
40 |
| - count = sum; |
41 |
| - word = wordx; |
| 57 | + |
| 58 | + if (maxCt === 1) { |
| 59 | + return -1; |
| 60 | + } else { |
| 61 | + return maxWord; |
42 | 62 | }
|
43 |
| - } |
44 |
| - if (count > 0) { |
45 |
| - return word; |
46 |
| - } else{ |
47 |
| - return "-1"; |
48 |
| - } |
49 |
| - |
50 | 63 | }
|
0 commit comments