Given a string, what an efficient way is there to
- return the N-th lowest char in it (ASCII wise)?
- return the N-th lowset char*s* in it?
I have thought of sorting N iteration with insertion sort, or sorting quick sort, where the pivot is in the N-th position, but I had problem analyzing time complexity. Is there any other, more efficient way?
What will be the solution is it wasn’t a string, but a list of decimals?
If your strings are ASCII, you can do it using a counting-sort style algorithm. Make an array of 256 counters, go through the string, and increment a counter for each character’s code that you find in the string. Now walk the array from zero, accumulating the counts that you’ve seen so far. When adding a counter for character
chcauses your accumulated result cross overN, you know thatchis then-th character if the string were sorted. This algorithm isO(N), whereNis the number of characters in your string. This is the time that it takes to build the array of counts.