AP-1.4_21BCS4422_Manohar Chaudhary_ FL-603

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.4
Student Name: Manohar Chaudhary UID: 21 BCS4422
Branch: BE-CSE Section/Group: FL-603-B
Semester: 6th Date of Performance: 30-01-2024
Subject Name: Advance Programming-2 Subject Code: 21CSP-251

1. Aim: To demonstrate the concept of Hashing.

I. Problem statement – Given a string s, find the length of the longest Substring without
repeating characters. Input: s = "abcabcbb"
II. Problem statement – You are given a string s. You can convert s to a Palindrome by
adding characters in front of it. Return the shortest palindrome you can find by
performing this transformation. Input: s = "aacecaaa"

2. Objective:
• Finding the length of the longest substring without repeating characters.
• Constructing the shortest palindrome from a given string by adding characters to its
beginning.

3. Code and output:


1st:
class Solution:
def longestDupSubstring(self, s: str) -> str:
left = 0
right = 1
res = ""
n = len(s)
while right<n:
if s[left:right] in s[left+1:]:
if right - left > len(res):
res = s[left:right]
right+=1
continue
left+=1
if left == right:
right+=1
return res
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

2nd:

class Solution:
def shortestPalindrome(self, s: str) -> str:
i=0
n = len(s)
for j in range(n):
if s[i] == s[n-j-1]:
i += 1
if i==n:
return s
p = s[i:n][::-1]
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Learning Outcomes:

✓ Understood how to manipulate substrings to solve various problems.


✓ Learned methods to find the shortest palindrome by adding characters to the beginning of a
string.
✓ Learned how hashing maps data keys to values efficiently.
✓ Learned techniques like hashing, two-pointers, and dynamic programming for string
manipulation tasks.

You might also like