Sno 717
Sno 717
Sno 717
Assignment 2
Objective: The objective is to find the minimum number of steps to make the string s
a palindrome by inserting characters at any position.
Approach: The approach is based on dynamic programming. The key idea is to find
the longest palindromic subsequence (LPS) in the string s. The minimum number of
insertions required is the difference between the length of the string s and the length
of the LPS.
Algorithm:
1. Initialize:
2. Base Case:
4. Result:
Code:
class Solution:
def minInsertions(self, s: str) -> int:
n = len(s)
dp = [[0] * n for _ in range(n)]
for i in range(n):
dp[i][i] = 1
return n - dp[0][n - 1]
Output:
Algorithm:
Code:
class Solution:
def __init__(self):
self.res = []
Output:
Objective: Return a list of groups where each group contains strings that are
anagrams of each other.
Approach: Use a hash map to efficiently group anagrams based on their sorted
character representation.
Algorithm:
Use a hash map (dict in Python) where the key is the sorted version of each
string (sorted characters), and the value is a list of strings that match this sorted
key.
2. Group Anagrams:
Convert the values of the hash map (lists of anagrams) into a list and return it.
Code:
class Solution:
def groupAnagrams(self, strs):
mp = defaultdict(list)
for x in strs:
word = ''.join(sorted(x))
mp[word].append(x)
return list(mp.values())
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Output:
Approach: Use a hash map to keep track of cumulative sums modulo k encountered
so far. If we encounter the same cumulative sum modulo k again, it implies that the
subarray between these two indices has a sum that is a multiple of k.
Algorithm:
1. Initialize Variables:
Use a hash map mod_k to store cumulative sums modulo k and their
corresponding indices.
Initialize prefix to 0 to track the cumulative sum as we iterate through nums.
Initialize mod_k[0] = -1 to handle cases where the entire prefix sum up to the
current index is a multiple of k.
3. Return Result:
Code:
class Solution:
@staticmethod
def checkSubarraySum(nums, k):
n = len(nums)
if n < 2:
return False
mod_k = {}
prefix = 0
mod_k[0] = -1
for i in range(n):
prefix += nums[i]
prefix %= k
if prefix in mod_k:
if i > mod_k[prefix] + 1:
return True
else:
mod_k[prefix] = i
return False
Output:
Aim: Find the longest word in the array words that can be formed by concatenating
other words from the same array, character by character from left to right.
Objective: Return the longest word with the smallest lexicographical order if there
are multiple candidates, or an empty string if no such word exists.
Approach: Use a set (built) to keep track of words that can be constructed so far, and
iterate through words to check if each word can be formed by appending characters to
previously formed words.
Algorithm:
o Check if the prefix (all characters except the last one) of the current word
exists in built.
o If yes, add the current word to built.
As you iterate through words, update the longest_word variable whenever you
find a longer word that can be built.
4. Return Result:
Return the longest_word found, which is the longest word that can be built
from other words in words.
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Code:
class Solution:
def longestWord(self, words):
words.sort()
built = set()
res = ""
for w in words:
if len(w) == 1 or w[:-1] in built:
if len(w) > len(res):
res = w
built.add(w)
return res
Output: