Solving a leetcode problem Day5/10
Question 451 - Sort Characters By Frequency Difficulty-Medium
Hello Guys, Today is day 5 of #I4G10daysofcode organised by @IngressiveforGood and I'm here to tell you guys how it went and the challenges I faced while solving the problem/question that was given.
This article/blog would be centered around myself and my thoughts rather than the solution/hints to solving the task
So let's begin .
Today's task was a medium difficulty level task and the question was as follows
Given a string s, sort it in decreasing order based on the frequency of the characters. The frequency of a character is the number of times it appears in the string.
Return the sorted string. If there are multiple answers, return any of them.
Simple right? Well, not exactly.
You see this simple question seemed to be a little challenging for me. I presumed it would be easier for me to solve it using php, but i decided to use javascript to solve it (I'm still improving on my javascript btw!).
So i began.
Approach Vs Failure
My approach was to start by converting all the individual letters in the string to an array, reverse sort it and then loop through all the individual items on the array twice to check their level of occurence. Then save their level of occurence in another array with a key (map). I didn't know how to use js maps then but i was convasant with associative arrays in php. So I started by making research on the topic, " map in js". Learnt how to initialize a map , add items to a map,check size of a map, return value from a map based off its key , and get map key from its value.
Next my trials began. Failure after failure. 2 hours had gone by and still no success in sight.
###Success and TimeOut! Then i decide to give up on js and move back to the language i was comfortable coding in......PHP.
I followed the same approach, wrote the codes required to perform the given task in an online editor so i can test it extensively. After a while, I was able to write the perfect code to solve the problem. I tested the code with multiple test cases while on the online php editor. They were all successful. I was happy at least. Decided to run in leetcode using the "Run code" Button , Ran successfully. I was happy.
Now all that was left was to submit button which i did with all boldness.๐.
TimeOut
Time Limit Exceeded What went wrong?
My code ran right? No.
Found out one of the test case required that the solution is tested with very long string of characters. Thats why i was having such problem submitting . Tried optimizing my code and retrying for a while, but no success
Back to js
Had to revert back to js with the hope of doing some magic with my codes which i had previously abandoned. Trial and Trial after error Until at last! It compiled successfully!
Omo, I just jeje screenshot my thing. I had suffered enough.
Conclusion
Well that's all for now, if you enjoyed this post , Please give it a thumbs up.
You can also follow me on Twitter and Linkedin and until next time bye for now!