Created
June 19, 2017 09:26
-
-
Save AxelUser/c0d50b6c38fdf3207451479e5235c332 to your computer and use it in GitHub Desktop.
Count letters in substring.
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
/** | |
* Count letters in substring. | |
*/ | |
function strInput(str) { | |
var letterMap = {}; | |
var letterArray = []; | |
for(var index = 0; index < str.length; index++){ | |
if(letterMap[str[index]] !== undefined){ | |
letterMap[str[index]].count++; | |
} else { | |
letterMap[str[index]] = {count : 1}; | |
letterArray.push({ | |
counter: letterMap[str[index]], | |
letter: str[index] | |
}); | |
} | |
} | |
return letterArray.reduce((res, item) => { | |
res = res.concat(item.letter); | |
if(item.counter.count > 1){ | |
res = res.concat(item.counter.count); | |
} | |
return res; | |
}, ""); | |
} | |
var testStr = "AAAAAAABBBBBBBCCCFKRBBBBBAAAC"; | |
var outStr = strInput(testStr); | |
console.log(outStr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment