🚀 Solving 3 LeetCode Problems in Python: String Manipulation, Hashing, and Data Structures
Consistency is the key to becoming a better software engineer. As part of my daily Data Structures and Algorithms (DSA) practice, I solved three LeetCode problems that strengthened my understanding of string manipulation, hash maps, and designing efficient data structures.
📌 Problems Solved Today
1️⃣ Length of Last Word (LeetCode #58)
Problem:
Given a string consisting of words and spaces, return the length of the last word.
Approach:
I removed any trailing spaces using strip(), split the string into words, and returned the length of the last word.
Python Solution
class Solution:
def lengthOfLastWord(self, s: str) -> int:
return len(s.strip().split()[-1])
Time Complexity: O(n)
Space Complexity: O(n)
2️⃣ Max Number of K-Sum Pairs (LeetCode #1679)
Problem:
Given an integer array nums and an integer k, return the maximum number of operations where each operation removes two numbers whose sum equals k.
Approach:
I used a Hash Map (dictionary) to keep track of previously seen numbers. For every element, I searched for its complement (k - num). If found, I formed a valid pair; otherwise, I stored the current number for future matching.
Python Solution
from collections import defaultdict
class Solution:
def maxOperations(self, nums, k):
count = defaultdict(int)
operations = 0
for num in nums:
complement = k - num
if count[complement] > 0:
operations += 1
count[complement] -= 1
else:
count[num] += 1
return operations
Time Complexity: O(n)
Space Complexity: O(n)
3️⃣ Insert Delete GetRandom O(1) (LeetCode #380)
Problem:
Design a data structure that supports insertion, deletion, and returning a random element in average O(1) time.
Approach:
The optimal solution combines:
- A list for storing elements.
- A hash map to store the index of each element.
This combination enables constant-time insertion, deletion (by swapping with the last element), and random access.
Python Solution
import random
class RandomizedSet:
def __init__(self):
self.nums = []
self.pos = {}
def insert(self, val: int) -> bool:
if val in self.pos:
return False
self.pos[val] = len(self.nums)
self.nums.append(val)
return True
def remove(self, val: int) -> bool:
if val not in self.pos:
return False
idx = self.pos[val]
last = self.nums[-1]
self.nums[idx] = last
self.pos[last] = idx
self.nums.pop()
del self.pos[val]
return True
def getRandom(self) -> int:
return random.choice(self.nums)
Time Complexity
- Insert: O(1)
- Remove: O(1)
- GetRandom: O(1)
Space Complexity: O(n)
💡 Key Takeaways
✔️ Improved string manipulation techniques.
✔️ Strengthened understanding of Hash Maps for efficient lookups.
✔️ Learned how combining arrays and hash maps enables constant-time operations.
✔️ Reinforced the importance of choosing the right data structure for optimized solutions.
Every LeetCode problem is an opportunity to think critically, write cleaner code, and improve algorithmic problem-solving skills. Small, consistent improvements today lead to significant growth tomorrow.
If you're also practicing DSA, I'd love to connect and discuss different approaches to solving problems. Happy coding! 🚀
Top comments (0)