🧠5 LeetCode Problems Explained with Code + Video
Part of the DSA Decoded series — step-by-step Python solutions with video walkthroughs.
Problems covered in this post:
- 🟡 Word Search (LeetCode #79) · Medium · Backtracking
- 🟡 Implement Trie (Prefix Tree) (LeetCode #208) · Medium · Trie
- 🟡 Palindromic Substrings (LeetCode #647) · Medium · Dynamic Programming
- 🟡 Decode Ways (LeetCode #91) · Medium · Dynamic Programming
🟡 Word Search
Difficulty: Medium  | Topic: Backtracking  | LeetCode #79
Problem
Given an m x n grid of characters board and a string word, return true if word exists in the grid. The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Solution
def exist(board, word):
rows, cols = len(board), len(board[0])
path = set()
def dfs(r, c, i):
if i == len(word):
return True
if (r < 0 or c < 0 or r >= rows or c >= cols
or word[i] != board[r][c] or (r, c) in path):
return False
path.add((r, c))
res = (dfs(r+1, c, i+1) or dfs(r-1, c, i+1) or
dfs(r, c+1, i+1) or dfs(r, c-1, i+1))
path.remove((r, c))
return res
for r in range(rows):
for c in range(cols):
if dfs(r, c, 0):
return True
return False
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🟡 Implement Trie (Prefix Tree)
Difficulty: Medium  | Topic: Trie  | LeetCode #208
Problem
A trie (pronounced as 'try') or prefix tree is a tree data structure used to efficiently store and retrieve keys in a dataset of strings. Implement the Trie class with insert(word), search(word) that returns true if word is in the trie, and startsWith(prefix) that returns true if there is any word in the trie that starts with the given prefix.
Solution
class Trie:
def __init__(self):
self.children = {}
self.is_end = False
def insert(self, word):
node = self
for ch in word:
if ch not in node.children:
node.children[ch] = Trie()
node = node.children[ch]
node.is_end = True
def search(self, word):
node = self
for ch in word:
if ch not in node.children:
return False
node = node.children[ch]
return node.is_end
def starts_with(self, prefix):
node = self
for ch in prefix:
if ch not in node.children:
return False
node = node.children[ch]
return True
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🟡 Palindromic Substrings
Difficulty: Medium  | Topic: Dynamic Programming  | LeetCode #647
Problem
Given a string s, return the number of palindromic substrings in it. A string is a palindrome when it reads the same backward as forward. A substring is a contiguous sequence of characters within the string.
Solution
def count_substrings(s):
count = 0
def expand(l, r):
nonlocal count
while l >= 0 and r < len(s) and s[l] == s[r]:
count += 1
l -= 1
r += 1
for i in range(len(s)):
expand(i, i) # odd length
expand(i, i + 1) # even length
return count
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🟡 Decode Ways
Difficulty: Medium  | Topic: Dynamic Programming  | LeetCode #91
Problem
A message containing letters from A-Z can be encoded into numbers using 'A' -> '1', 'B' -> '2', ..., 'Z' -> '26'. Given a string s containing only digits, return the number of ways to decode it. If there are no valid ways to decode the message, return 0.
Solution
def num_decodings(s):
dp = {len(s): 1}
for i in range(len(s) - 1, -1, -1):
if s[i] == '0':
dp[i] = 0
else:
dp[i] = dp[i + 1]
if (i + 1 < len(s) and
(s[i] == '1' or s[i] == '2' and s[i + 1] in '0123456')):
dp[i] += dp.get(i + 2, 1)
return dp[0]
🔗 Solve on LeetCode  · 📺 Watch video explanation  · 💻 Full solution + tests on GitHub
🔔 Follow Along
New problems every 2 days — subscribe to stay consistent with your DSA prep!
Drop a comment with your approach — always curious to see other solutions!
Top comments (0)