DSA _Strings_ Notes
DSA _Strings_ Notes
[LINK IN POST]
Easy:
1. https://leetcode.com/problems/remove-outermost-parentheses/description/
2. https://leetcode.com/problems/reverse-words-in-a-string/description/
3. https://leetcode.com/problems/largest-odd-number-in-string/description/
4. https://leetcode.com/problems/longest-common-prefix/description/
5. https://leetcode.com/problems/isomorphic-strings/description/
6. https://leetcode.com/problems/rotate-string/description/
7. https://leetcode.com/problems/valid-anagram/description/
Medium:
1. https://leetcode.com/problems/sort-characters-by-frequency/description/
2. https://leetcode.com/problems/maximum-nesting-depth-of-the-parentheses/
3. https://leetcode.com/problems/roman-to-integer/description/
4. https://leetcode.com/problems/longest-palindromic-substring/description/
5. https://leetcode.com/problems/sum-of-beauty-of-all-substrings/description/
6. https://leetcode.com/problems/string-to-integer-atoi/description/
Hard:
1. https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/
2. https://leetcode.com/problems/count-and-say/description/
3. https://leetcode.com/problems/repeated-string-match/description/
4. https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/
5. https://leetcode.com/problems/shortest-palindrome/description/
6. https://leetcode.com/problems/longest-happy-prefix/description/
Basic Definition
A sequence of characters stored in a contiguous memory location, terminated by a special
character called null character ‘\0’.
Characteristics
1. Ordered: Each character has a unique position in the string.
2. Indexable: Individual characters within a string can be accessed using an index.
3. Comparable: They can be compared with each other for equality.
Operations on String
1. Size(): Used to find the length of the string.
2. Substr(): This is used to create a substring of a given length.
3. +: Used to concatenate two strings.
4. s1.compare(s2): Used to compare two strings, to compare which is lexicographically
greater and which is smaller.
5. reverse(): Reverse a string
6. sort(): Sort a string in alphabatical order.
int main() {
string str = "Hello, World!";
cout << str.length() << endl;
string s2 = s1 + "also";
string res;
for (int i = words.size() - 1; i >= 0; i--) {
res += words[i];
if (i != 0) {
res += " ";
}
}
return res;
}
Prefix vs Suffix
They are the characters of the string from the start or end, here is an example.
Word = “Bread”
Prefixes = “B”, “Br”, “Bre”, “Brea”, “Bread”
Suffixes = “d”, “ad”, “ead”, “read”, “Bread”
Isomorphic Strings
All occurrences of a character must be replaced with another character while preserving the
order of characters.
s1 = “egg”
s2 = “foo”
Result = Isomorphic
“string::npos”
This special constant represents “no position in the string.” String functions return it when a
substring, character, or search pattern is not found.
s1 = “anagram”
s2 = “nagaram”
Result = Anagram
Substring of a String
It is a contiguous sequence of characters within that string.
Subset of a String
It refers to any combination of characters from the string, without considering their order or
contiguity. Unlike substrings, subsets do not require the characters to be adjacent in the
original string.
Subsequence of a String
It is a a sequence of characters that can be derived from the original string by deleting some
or no characters without changing the order of the remaining characters.
Run-Length Encoding
It is a string compression method that works by replacing consecutive identical characters
with the concatenation of the character and the number marking the count of the character.
str = “3322251”
2 -> 3s 1 -> 5s
3 -> 2s 1 -> 1s
Z Function
str = “ababbabcde”
z array = 0 0 2 0 0 2 0 0 0
This tells how many characters match the full string starting from the current character. It can
be used for string matching. For example, "aba$ababbababaaa" has “aba,” which you have
to find in “ababbababaaa,” both separated by a “$”.
vector<int> zMatch(string s) {
vector<int> z(s.size(), 0);
int l = 0, r = 0;
for (int i = 1; i < s.size(); i++) {
if (i < r) {
z[i] = z[i - l];
// Handle case of z[i] reaching for characters
// beyond what we have seen at r
if (i + z[i] > r) {
z[i] = r - i;
}
}
// Update l, r
if (i + z[i] > r) {
l = i;
r = i + z[i];
}
}
return z;
}
int main() {
vector<int> z = zMatch("aba$ababbababaaa");
for (auto it: z) cout << it << " ";
cout << endl;
return 0;
}
#include <bits/stdc++.h>
using namespace std;
/* Driver code */
int main()
{
char txt[] = "GEEKS FOR GEEKS";
char pat[] = "GEEK";
int q = 101; // A prime number
search(pat, txt, q);
return 0;
}
KMP Algorithm
Refer to : https://www.youtube.com/watch?v=V5-7GzOfADQ
// Function to preprocess the pattern and create the LPS (Longest Prefix
Suffix) array
void computeLPSArray(const string& pattern, vector<int>& lps) {
int length = 0; // Length of the previous longest prefix suffix
lps[0] = 0; // lps[0] is always 0
int i = 1;
while (i < n) {
if (pattern[j] == text[i]) {
i++;
j++;
}
if (j == m) {
cout << "Pattern found at index " << i - j << endl;
j = lps[j - 1];
} else if (i < n && pattern[j] != text[i]) {
if (j != 0) {
j = lps[j - 1];
} else {
i++;
}
}
}
}
int main() {
string text = "ababcabcabababd";
string pattern = "ababd";
KMPSearch(text, pattern);
return 0;
}