0% found this document useful (0 votes)
7 views6 pages

APExp4 tekrat

The document outlines an experiment conducted by a student in an Advanced Programming Lab, focusing on string manipulation problems. It includes three problems with aims, objectives, implementations, and time/space complexities, along with learning outcomes related to string operations and pattern matching algorithms. The student implemented solutions in C++ to demonstrate efficient string handling techniques.

Uploaded by

XZEEZ FF
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)
7 views6 pages

APExp4 tekrat

The document outlines an experiment conducted by a student in an Advanced Programming Lab, focusing on string manipulation problems. It includes three problems with aims, objectives, implementations, and time/space complexities, along with learning outcomes related to string operations and pattern matching algorithms. The student implemented solutions in C++ to demonstrate efficient string handling techniques.

Uploaded by

XZEEZ FF
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/ 6

Experiment - 4

Student Name: Tekrat Prajapati UID: 22BCS13545


Branch: BE - CSE Section/Group: IOT_635- A
Semester: 6 Sub Code: 22CSP-351
Subject Name: Advanced Programming Lab - 2 Date: 16/01/2025

Problem - 1
Aim - To determine whether a given string s can be transformed into another string goal by
performing a series of cyclic left shifts, where each shift moves the leftmost character of s to
the rightmost position.

Objective - To determine whether a given string s can be transformed into another string goal by
performing a series of cyclic left shifts.

Implementation/Code -

class Solution {
public:
bool rotateString(string s, string goal) {

if (s.size() != goal.size())
return false;
if ((s + s).find(goal) != string::npos)
return true;
else
return false;
}
};
Output -

Time Complexity - O(n) Space Complexity - O(n )

PROBLEM – 2

Aim - To find the index of the first occurrence of a substring (needle) within a given string
(haystack) or return -1 if the substring is not present.

Objective - To implement an efficient algorithm that searches for needle in haystack using built-in
functions or string-matching algorithms like Brute Force, Knuth-Morris-Pratt (KMP), or Boyer-
Moore to achieve optimal performance.

Implementation/Code -
class Solution {
public:
int strStr(string haystack, string
needle) {
size_t pos = haystack.find(needle);
return (pos != string::npos) ? pos : 1;
}
};
Output -

Time Complexity - O(n*m) Space Complexity - O(1)


PROBLEM – 3

Aim - To determine the minimum number of times a string a must be repeated such that
another string b becomes a substring of the repeated string

Objective - To implement an efficient algorithm that finds the minimal repetitions


of a required to contain b as a substring, using string operations optimally.

Implementation/Code -
class Solution {
public:
int repeatedStringMatch(string a, string b) {
string repeated = a;
int count = 1;

// Repeat `a` until its length is at least the length of `b`


while (repeated.length() < b.length()) {
repeated += a;
count++;
}
if (repeated.find(b) != string::npos)
return count;

/ / One more repetition might be needed


repeated += a;
count++;

if (repeated.find(b) != string::npos)
return count;

return -1;
}
};

Output -

Time Complexity - O(n*m) Space Complexity - O(n*m)

Learning Outcomes -
1️. String Manipulation: Learned how to efficiently rotate, concatenate, and search
substrings in a given string.
2️. Pattern Matching Algorithms: Explored KMP Algorithm to optimize substring search,
reducing time complexity from O(NM) to O(N + M).
3️. Optimized Searching Techniques: Used find(), brute force, and KMP to detect substring
occurrences efficiently.
4️. Understanding LPS (Longest Prefix Suffix) Array: Implemented LPS to avoid
redundant comparisons in KMP pattern matching.
5️. Algorithm Efficiency & Complexity Analysis: Compared brute-force, built-in functions,
and KMP for optimal string processing.

You might also like