Week 5 of Leetcode!

Parker Williamson
3 min readJan 26, 2021

This is the last week of Leetcode posts I am happy to say! I have reconnected with a classmate and we are going to go through our curriculum again to really hammer down the basics of Ruby which is going to be amazing for me. Going through basic iteration and pausing the curriculum to build even more advanced concepts from it would help me advance so much in the process of learning and becoming better at Ruby, and hopefully landing the dream job sooner!

My first leetcode this week was the Maximum 69 number leetcode. For this I just converted to a string, and it was stored in an index, and converted back to an integer. The code for this was really simple but I did have to use a little bit of outside source for the code.

#Maxiumum 69 Number# @param {Integer} num# @return {Integer}def maximum69_number (num)new_a = num.to_snew_a[new_a.index("6")] = "9" if new_a.index("6") != nilnew_a.to_iend

My next problem was generating a string with characters that have an odd count. This problem was a lot similar to the former problem but it didn’t need any conversion. I set resul to ‘a’ * n, checked if even, then printed the result following.

#Generate a String With Characters That Have Odd Counts# @param {Integer} n# @return {String}def generate_the_string(n)result = 'a' * nresult[0] = 'b' if n.even?resultend

My third problem was Find Words Formed by Characters. This one was definitely the more difficult one to handle but it wasn’t the worst. I created an array, iterated over each of the words in the strings provided, and broke the iteration if the count of i was larger than the count of characters.

#Find Words Formed By Characters# @param {String[]} words# @param {String} chars# @return {Integer}def count_characters(words, chars)final = []words.each do |i|s = 0i.each_char do |j|break if i.count(j) > chars.count(j)s+=1endfinal << i if s == i.lengthendproduct = 0final.each do |i|product += i.lengthendp productend

My final problem was finding three consecutive odds, and that was a pretty difficult one although it shouldn’t have been. I just chose to return false if the array length was less than 3, and then iterated through the numbers I have.

#Three Consecutive Odds# @param {Integer[]} arr# @return {Boolean}def three_consecutive_odds(arr)return false if arr.length < 3for i in 2..arr.length-1if (arr[i]%2==1) and (arr[i-1]%2==1) and (arr[i-2]%2==1)return trueendendfalseend

My final github commit was just a fix to a previous project where I changed the original information on the main page of my League of Legends React final project. Leetcode has been getting stale for me as of now, because I have finished most of the problems in my scope for the easy problems, and the medium is a little bit further than my skill level which is why I chose to further my education using the curriculum provided by my school. I am happy to check in next week and show what I have learned!

--

--