Created
July 22, 2019 05:16
-
-
Save kylejeske/285ddbd0eb24b19948841a1fc2afeb67 to your computer and use it in GitHub Desktop.
Return the longest sequence of consecutive characters in string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// runner (s: string) | |
// return -- expects: Object { maxChar: numberOfOccurances } | |
const runner = (s="") => { | |
let stringLength = (s.length - 1); | |
let position = 0; | |
let currentChar = ""; | |
let maxCount = 0; | |
let count = 0; | |
let maxChar = null; | |
let prevChar = null; | |
while(position < stringLength) { | |
currentChar = s[position]; | |
// if prev character and current character are same, increment | |
// otherwise set the count to 1 | |
if (prevChar == currentChar) { | |
count += 1; | |
} else { | |
count = 1; | |
} | |
// if our current count is greatst, then it is now the largest count | |
if (count > maxCount) { | |
maxCount = count; | |
maxChar = currentChar; | |
} | |
prevChar = currentChar; | |
position++; | |
} | |
// because we expect an object to be returned | |
// set it up as such | |
let dict = {}; | |
dict[maxChar] = maxCount; | |
return dict; | |
} | |
let maxCharInString = runner("AABCDDBBBEA"); | |
// will return { B: 3 } | |
console.log(maxCharInString); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment