11. Count Words
Required Input:
s = 'Hello world, this is a test.'Expected Output:
6
Code In Python
def count_words(s):
# Write your logic here
pass
# Prefilled input
s = "Hello world, this is a test."
print(count_words(s))
Run Code?
Click Run Button to view compiled output
12. Basic Trie
Required Input:
words = ['apple', 'app', 'apply'], search_word = 'app'Expected Output:
True
Code In Python
class Trie:
def __init__(self):
# Initialize Trie here
pass
def insert(self, word):
# Insert a word into Trie
pass
def search(self, word):
# Search for a word in Trie
pass
# Prefilled input
trie = Trie()
words = ["apple", "app", "apply"]
for word in words:
trie.insert(word)
print(trie.search("app"))
Run Code?
Click Run Button to view compiled output
13. String Rotation
Required Input:
s1 = 'waterbottle', s2 = 'erbottlewat'Expected Output:
True
Code In Python
def is_rotation(s1, s2):
# Write your logic here
pass
# Prefilled input
s1 = "waterbottle"
s2 = "erbottlewat"
print(is_rotation(s1, s2))
Run Code?
Click Run Button to view compiled output
14. Longest Palindromic Substring
Required Input:
s = 'babad'Expected Output:
bab
Code In Python
def longest_palindromic_substring(s):
# Write your logic here
pass
# Prefilled input
s = "babad"
print(longest_palindromic_substring(s))
Run Code?
Click Run Button to view compiled output
15. Minimum Window Substring
Required Input:
s1 = 'ADOBECODEBANC', s2 = 'ABC'Expected Output:
BANC
Code In Python
def min_window_substring(s1, s2):
# Write your logic here
pass
# Prefilled input
s1 = "ADOBECODEBANC"
s2 = "ABC"
print(min_window_substring(s1, s2))
Run Code?
Click Run Button to view compiled output
16. Word Search using Trie
Required Input:
board, words = ['oath', 'pea', 'eat', 'rain']Expected Output:
['oath', 'eat']
Code In Python
class TrieNode:
def __init__(self):
self.children = {}
self.word = None
class WordSearch:
def __init__(self):
self.root = TrieNode()
def insert(self, word):
# Insert words into Trie
pass
def find_words(self, board, words):
# Search words in board using Trie
pass
# Prefilled input
board = [
["o", "a", "a", "n"],
["e", "t", "a", "e"],
["i", "h", "k", "r"],
["i", "f", "l", "v"]
]
words = ["oath", "pea", "eat", "rain"]
ws = WordSearch()
for word in words:
ws.insert(word)
print(ws.find_words(board, words))
Run Code?
Click Run Button to view compiled output
17. String Permutations
Required Input:
s = 'abc'Expected Output:
['abc', 'acb', 'bac', 'bca', 'cba', 'cab']
Code In Python
def generate_permutations(s):
# Write your logic here
pass
# Prefilled input
s = "abc"
print(generate_permutations(s))
Run Code?
Click Run Button to view compiled output
18. KMP Pattern Search
Required Input:
text = 'ababcabcab', pattern = 'abc'Expected Output:
[2, 5]
Code In Python
def kmp_search(text, pattern):
# Write your logic here
pass
# Prefilled input
text = "ababcabcab"
pattern = "abc"
print(kmp_search(text, pattern))
Run Code?
Click Run Button to view compiled output
19. Auto-complete System
Required Input:
prefix = 'he'Expected Output:
['heap', 'helicopter', 'hell', 'hello', 'help', 'hero']
Code In Python
class TrieNode:
def __init__(self):
self.children = {}
self.word_list = []
class AutoComplete:
def __init__(self):
# Initialize Trie here
pass
def insert(self, word):
# Insert word into Trie
pass
def get_suggestions(self, prefix):
# Return words with given prefix
pass
# Prefilled input
ac = AutoComplete()
words = ["hello", "hell", "helicopter", "hero", "help", "heap"]
for word in words:
ac.insert(word)
print(ac.get_suggestions("he"))
Run Code?
Click Run Button to view compiled output
20. Shortest Unique Prefix
Required Input:
words = ['dog', 'dove', 'duck']Expected Output:
{'dog': 'dog', 'dove': 'dov', 'duck': 'du'}
Code In Python
class TrieNode:
def __init__(self):
self.children = {}
self.freq = 0
class ShortestPrefix:
def __init__(self):
# Initialize Trie here
pass
def insert(self, word):
# Insert word into Trie and track frequency
pass
def get_shortest_prefix(self, word):
# Return shortest unique prefix
pass
def find_unique_prefixes(self, words):
# Find unique prefixes for all words
pass
# Prefilled input
words = ["dog", "dove", "duck"]
sp = ShortestPrefix()
for word in words:
sp.insert(word)
print(sp.find_unique_prefixes(words))
Run Code?
Click Run Button to view compiled output


