APExp4 tekrat
APExp4 tekrat
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 -
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 -
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
Implementation/Code -
class Solution {
public:
int repeatedStringMatch(string a, string b) {
string repeated = a;
int count = 1;
if (repeated.find(b) != string::npos)
return count;
return -1;
}
};
Output -
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.