0% found this document useful (0 votes)
19 views3 pages

Ap 2 Exp 1.4

This document summarizes Experiment 1.4 conducted by student Tushar Anand. The aim was to demonstrate the concept of hashing. It contains solutions to two questions - 1) find the shortest palindrome string that can be formed by adding characters in front of a given string, and 2) find the length of the longest substring in a string without repeating characters. The solutions implement the KMP and hashing algorithms respectively. The learning outcomes include applying KMP approach, implementing a basic hashing program, and optimizing code for time and space complexity.

Uploaded by

Tushar Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Ap 2 Exp 1.4

This document summarizes Experiment 1.4 conducted by student Tushar Anand. The aim was to demonstrate the concept of hashing. It contains solutions to two questions - 1) find the shortest palindrome string that can be formed by adding characters in front of a given string, and 2) find the length of the longest substring in a string without repeating characters. The solutions implement the KMP and hashing algorithms respectively. The learning outcomes include applying KMP approach, implementing a basic hashing program, and optimizing code for time and space complexity.

Uploaded by

Tushar Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

DEPARTMENT OF

COMPUTER SCIENCE & ENGINEERING

Experiment 1.4
Student Name: Tushar Anand UID: 22BCS90001
Branch: CSE Section/Group: CC_635-A
Semester: 6th Date of Performance: 20/02/2024
Subject Name: AP LAB - 2 Subject Code: 21CSP-351

1. AIM: To demonstrate the concept of Hashing.


2. OBJECTIVE:
Question 1: 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.
Question 2: Given a string s, find the length of the longest substring
without repeating characters.
3. SCRIPT & OUTPUT:
Solution 1:
class Solution {
public:
void computelps(vector<int>&lps,string s,int m)
{
int len=0,i=1;
while(i<m)
{
if(s[len]==s[i])
{
len++;
lps[i]=len;
i++;
}
else
{
if(len!=0) len=lps[len-1];
else
{
lps[i]=0;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

i++;
}
}
}
}
string shortestPalindrome(string s) {
int n=s.size();
string ans=s;
s+='#';
for(int i=n-1;i>=0;i--)
{
s+=s[i];
}
vector<int>lps(2*n+2,0);
computelps(lps,s,n+n+1);
string temp="";
for(int i=n-1;i>=lps[2*n];i--)
{
temp+=s[i];
}
return temp+ans;
}
};
Output:
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING

Solution 2:
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int n = s.length();
int maxLength = 0;
vector<int> charIndex(128, -1);
int left = 0;
for (int right = 0; right < n; right++) {
if (charIndex[s[right]] >= left) {
left = charIndex[s[right]] + 1;
}
charIndex[s[right]] = right;
maxLength = max(maxLength, right - left + 1);
}
return maxLength;
}
};
Output:

4. LEARNING OUTCOME:
• Learned about how to use KMP approach in DSA problems.
• To implement a basic Hashing program to find shortest Palindrome.
• Apply problem-solving skills to optimize code for both time and space
complexity.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy