Pushing through Challenges. The Halfway Mark- #Day5

Leetcode Problems. Sorting Characters Based on Frequency (I4G 10 Days Of Code Challenge)

ยท

3 min read

The coding challenges I have faced for the past few days has made me to understand at a deeper level, how mentally difficult it is to show a computer how to solve problems that on my own would be considered a piece of cake and would require no conscious thought.

Anyway, let's go into today's challenge shall we?

The Challenge

5_question.png Looks simple enough...

Approach 1

The approach I had in mind was simple enough:

  1. Convert the inputted string to an array
  2. Sort the array in descending order
  3. Rearrange the sorted array to have the most frequent letter
  4. Convert the array back to a string
  5. Return the string

Using this process in mind, I started typing in my answer using PHP (my preferred language) and its inbuilt functions to support strings and arrays and came up with code that looked something like:

str_to_array = convert_to_array(string);

ksort(str_to_array);

array_to_string = convert_to_string("",$str_array);
return reverse_string($test_string);

if you look at the code I used the function ksort() which allowed me to arrange the array in descending order based on the key. Testing the code I made using the above pseudocode I passed the example test cases but when I submitted....

5_failed_2.png ๐Ÿ˜ž Well that didn't work

Final Approach

Seeing that the approach I had initially used failed, I decided to go back to the prompt to check whether I had missed something in the challenge prompt (like Day 4's challenge). From there I checked my code through an external compiler and thankfully, it did not take long to realize my error.

I had assumed that the sorting function took like letters and grouped them together (as my code worked in the test cases). Also, the prompt says to arrange the string in a descending order based on frequency, which I mistook as descending alphabetically.

So, instead of simply converting the string to a simple array, I converted the string to an associative array with the letter as the key and the frequency of the letter as the value, Something like this:

// Before conversion
string = "aaabbbbccd";

// After conversion
array = {[a] => 3, [b] => 4, [c] => 2, [d] = 1};

From there, I then used a different sorting function which did not only sort the array in descending order based on value (frequency), but also retained the array association. Using it on our array in this case will give us something like this:

// Before sorting function
array = {[a] => 3, [b] => 4, [c] => 2, [d] => 1};

// After sorting function
array = {[b] => 4, [a] => 3, [c] => 2, [d] => 1};

Finally, I then converted the array back to a string with each letter being repeated based on the associated frequency in the order of the sorting function. Using the example, I am working with:

// Before conversion
array = {[b] => 4, [a] => 3, [c] => 2, [d] => 1};

// After conversion
string = "bbbbaaaccd";

I tested the code and fortunately it worked.

5_accepted.png Yay!

ย