My Strategy for Studying Leetcode
First of all, don’t put pressure on yourself. Focus on the long term by doing a little every day or fitting it into your schedule and sticking to the plan, okay?
With that in mind, here is my strategy for studying Leetcode.
1. Start with the basics
At the moment, I’m studying the basics of algorithms, focusing on string manipulation on Leetcode by filtering for easy problems. In this post,
I’ll walk you through the 125. Valid Palindrome
exercise on Leetcode.
2. Study the problem
At this point, you should read and understand the problem. You can view the problem here.
3. Write the code
Now, I’ll start by explaining how I study on Leetcode. First, I focus on simply solving the problem without worrying about finding the most optimized solution. The goal is just to get a working solution, okay?
In this example, I start by solving the problem with the following code:
func isPalindrome(s string) bool {
var reversed string
var newS string
for _, v := range s {
if unicode.IsLetter(v) || unicode.IsDigit(v) {
reversed += strings.ToLower(string(v))
newS = strings.ToLower(string(v)) + newS
}
}
return reversed == newS
}
I noticed that this solution takes 1067 ms and uses 80.2 MB of memory
. At this point, I start looking for ways to improve it.
4. Optimize the code
Here, I work on improving the solution to make it faster and more memory-efficient:
func isPalindrome(s string) bool {
var stringReversed string
var sNew string
lenString := len(s)
count := 0
for count != lenString {
if unicode.IsLetter(rune(s[count])) || unicode.IsDigit(rune(s[count])) {
sNew += strings.ToLower(string(s[count]))
stringReversed = strings.ToLower(string(s[count])) + stringReversed
}
count++
}
return sNew == stringReversed
}
I observed that the updated solution now takes 976 ms and uses 48.5 MB of memory
.
At this point, I’ll continue refining it to see if further improvements are possible.
5. Last improve solution
Then I realized that to validate a palindrome, I don’t need to store anything in memory; I only need to compare the characters at specific positions. With this approach, my solution improved significantly.
func isPalindrome(s string) bool {
for i, j := 0, len(s)-1; i < j; {
if !isAlphaNumeric(s[i]) {
i++
} else if !isAlphaNumeric(s[j]) {
j--
} else if strings.ToLower(string(s[i])) != strings.ToLower(string(s[j])) {
return false
} else {
i++
j--
}
}
return true
}
func isAlphaNumeric(s byte) bool {
return (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || (s >= '0' && s <= '9')
}
I saw that this optimized solution now takes 0 ms and uses only 4.5 MB of memory
.
At this point, I’ll see if there’s any further room for improvement, though the solution is already very efficient.
Here a printscreen of the solutions on Leetcode:
6. Conclusion
In these steps, I’ve shown you my approach to studying on Leetcode:
First, I solve the problem without worrying about finding the optimal solution—just focusing on getting it to work. Next, I try to refine the solution to use less memory and run faster. Finally, I think about further simplifications. For example, to validate a palindrome, I realized I don’t need to store anything in memory; I only need to compare specific character positions. This approach greatly improved my solution. This method has helped me write better code in my daily work, and I hope it’s helpful for you too!
Happy coding! 🚀