Photo by Hulki Okan Tabak on Unsplash
Day 5: I4G 10 Days of Code Challenge
Big, bigger, biggest. (Thing is, I just love to play around with the article subtitle space😂.)
Today was another day away from JavaScript —not that I'm complaining; it definitely has helped brush up my python knowledge. I felt I could rationalise the absence of JavaScript from yesterday's problem but definitely not todays.
The Problem
The challenge for today was to sort a string by character frequency. Where a string (word, letter, sentence) like "utilitarian" would become "iiittaaulrn".
From the example, characters occurring the most are written first, then in second place are the next most occurring characters and so on. The big question then becomes how to write a program to do the sorting.
Coming up with an algorithm was fairly easy although I had to read up on some python syntax to convert the algorithm to code.
THOUGHT PROCESSS
Intuitively, I used a counter to go through the entire string, taking note of each character and how many times they occurred. Then, I stored this information in an array.
Finally, I sorted the array in descending order (biggest, bigger, big) and formed a new string from the result.
My Solution:
Because I traversed through the array once, the time complexity becomes: 0(n)
The space complexity is also 0(n)