Created
March 16, 2021 07:26
-
-
Save yairzaslavsky/25e168650bc76c92884b6173fd7a5cef to your computer and use it in GitHub Desktop.
n-th frequent letter (with stubs)
This file contains 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
/** Given a string return the index of the first apperance of the n-th most frequent letter | |
* If two letters are equally frequent return the matching index of any of the the two | |
* Examples: "aabbbc" , 3 will return 5 , "aabbbbc", 1 will return 2 | |
*/ | |
fun firstIndexOfNthMostFrequentChar(str: String, n: Int): Int { | |
val countsMap = createCountsMap(str) | |
if (countsMap.size < n) { | |
return -1 | |
} | |
val sortedKeyValuePairs = sortKeyValuePairs(countsMap) | |
val ch = sortedKeyValuePairs[n-1].first | |
return str.indexOf(char = ch, startIndex = 0) | |
} | |
fun sortKeyValuePairs(map: Map<Char, Int>): List<Pair<Char, Int>> { | |
return listOf() | |
} | |
fun createCountsMap(str: String): Map<Char, Int> { | |
return mapOf() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment