Intro to Data Structures and Algorithms using LeetCode

Daniel Costelloe
3 min readMay 13, 2021

Data Structures and Algorithms are important for many reasons. From the perspective of someone who is currently unemployed and living at his parents. It’s important because it’s probably what going to get you that dream job you want. Secondly it’s important to have a higher understanding of how things works and why they do work. We’ll be looking at a problem today and go over a couple solution to the problem.

https://leetcode.com/problems/to-lower-case/

This is a pretty good question to start with. Let’s keep it simple, The problem is asking us to take a string and return the string in lowercase. Not sure about you but looking at this problem my first thought is using .toLowerCase(). So let’s run that code and see if we past our tests.

https://leetcode.com/problems/to-lower-case/

Great! Looks like we past our tests and figured it out on a surface level using a built-in method from javascript. We even followed the first hint. This is a good start but let’s be honest, while keeping things simple is important, answering the question like this is a bit of a cop out so let’s dive a bit deeper and look at other ways to go about this.

https://leetcode.com/problems/to-lower-case/

Let’s take a look at the hints and see if that can point us in the right direction. Taking a look at this it’ll be a harder task than we originally thought. While I don’t have much time to explain what ASCII is right now. Let’s say it assigns a numerical value to letters of the alphabet. This is a tall order for now so let’s look at some solutions and hopefully we can gain some insights and use this as a learning experience.

https://leetcode.com/problems/to-lower-case/discuss/936869/JavaScript-Solution

While this is pretty confusing to look at first glance. If we look into it a little more we can see where the logic goes with this one. we’re setting an empty string as a response. Looping through the string and using the ascii variable to return it’s numerical value for that given string we iterate through. Reading the comments will provide some more insight as to why this does work. This is followed by an if statement checking where the ascii lives the numerical values and adding 32 to push that into our response, if it doesn’t fit we’re keeping that in our response as is. Then we can return our response and see how we do.

https://leetcode.com/problems/to-lower-case/

Not a surprise that using this logic gives us a longer run time but it’s good to know that’s what’s going on behind some of these built-in methods. While the difference in time might make no difference to me at the moment, it’s always interesting to see different solutions to the same problem. Problems like this definitely make me appreciate the simplicity of built-in methods because doing this every time we want to make a string lowercase would be a constant headache. If anyone wants to dive deeper than I feel free to check out more problems on the resource below.

https://leetcode.com/

--

--

Daniel Costelloe

Student at Flatiron school, Aspiring software developer