Try this: draw picture, write down or think about how would you explain/teach this to someone very young like a small kid then try implementing that on code like have a mindset of teaching/trying to explain things to someone. My experiences has been that most of the times we know how to do it ourselves but it is trying to teach it to a computer is what we struggle with. Make points if you can like
- First do this
- Then do this etc
For example, I think you and almost everyone in the world would know if you ask them whether these two strings are same right?
["hello", "Hello"]
You look and it and think alright, they both say hello but the difference is the h being capital.
So how would you teach it to a someone much younger than you?
-
first you take the first element of the array and compare it with second element while comparing ignore that the case of the letter that is whether its upper case or lower case. How would you do that? How would you treat Hello and hello as same? This could be tricky! Think, think. The solution is when you turn every element you compare to lower case. arr[0].toLowerCase or to uppercase then they basically become same. If they are same then return true.
-
If they are different then return false.
-
Now time to think a little hard. how would we teach someone much younger than us to say that these two are same too?
["Alien", "line"]
How would you do it. This is where you start to become a problem solver/puzzle solver. Think of it like a game, a fun game. Don’t stress and keep thinking.
I forgot how I solved it. But here is how I would do it.
First, I would check and point out which one is the shorter one. “Line” right?
Now I compare L to each letter of Alien, then I go to i then N then E
Of course lower casing it first.
l time to compare it with each letter of a l i e n does it match? yes
i time to compare it with each letter of a l i e n does it match? yes
n time to compare it with each letter of a l i e n does it match? yes
e time to compare it with each letter of a l i e n does it match? yes
If all the letters match then you would return true right? That’s how we humans would do. Actually different people would do it in different ways.
Now time to implement this logic into code.
How would you compare each letter of one string to each letter of other string. I think with str[i] where i goes from first to last letter of string 1 and then another loop where it goes through the another string. I will stress this here. Basics are important if you don’t know you need to go back and learn how to use for loop nested ones too if possible with paper and pen
Now next question is how would you add up the yesses if it match. You can set up a counter and add it every time it matches and at the end when the counter is equal to the length of the shorter string you return true. I’m not sure, this is fun part going and trying the logic, solving one bug after another. Once you start doing it, it can become tough but fun at the same time. For me it was almost addictive that I spent many hours a day doing it as I enjoyed it a lot
I hope you will enjoy it too even though its kind of hard.
And don’t beat yourself up algorithm scripting can be hard for everyone or take a while to go through. Work on it as much as possible instead of looking up the solutions.