0% found this document useful (0 votes)
10 views8 pages

DSA _Strings_ Notes

String ds notes

Uploaded by

tsdaku99
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)
10 views8 pages

DSA _Strings_ Notes

String ds notes

Uploaded by

tsdaku99
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/ 8

DSA “Strings”

Theory + Problem List

[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.

Time Complexity of Operations


1. Access Character at Index: O(1)
2. Find Length: O(1)
3. Get Substring: O(n)
4. Concatenation: O(n+m)
5. Replace/Delete: O(n)
6. Reverse: O(n)

Basic Code for Reference

int main() {
string str = "Hello, World!";
cout << str.length() << endl;

string s2 = s1 + "also";

//substring of length 5 from 7th index


cout << str.substr(7,5) << endl;

string check = "My name is Akshat!";


int pos = str.find('A');
if(pos != -1) {
cout << "Found in Position: " << pos << endl;
}
else {
cout << "Not Found!" << endl;
}
}
String Stream
This treats the string like a stream, similar to cin and cout using the <sstream> library. This is
useful when you want to iterate through the entire string including spaces. It uses the “>>”
operator to extract data from the stream till a whitespace is encountered.

//reversing all words of a string


string reverseWords(string s) {
stringstream ss(s);
string word;
vector<string> words;

while (ss >> word) {


words.push_back(word);
}

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.

return (str).find(goal) != string::npos;


Anagram
Two strings have the same characters, each having the same frequency but not necessarily
in the same position.

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

new str = 23 32 15 11 = “23321511”

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;
}
}

// Look for more batches beyond the current calculation


while(i + z[i] < s.size() && s[z[i]] == s[i + z[i]]) z[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;
}

String Hashing + Rabin Karp

#include <bits/stdc++.h>
using namespace std;

// d is the number of characters in the input alphabet


#define d 256
void search(char pat[], char txt[], int q) {
int M = strlen(pat);
int N = strlen(txt);
int i, j;
int p = 0; // hash value for pattern
int t = 0; // hash value for text
int h = 1;

// The value of h would be "pow(d, M-1)%q"


for (i = 0; i < M - 1; i++)
h = (h * d) % q;

// Calculate the hash value of the pattern and first


// window of text
for (i = 0; i < M; i++)
{
p = (d * p + pat[i]) % q;
t = (d * t + txt[i]) % q;
}

// Slide the pattern over the text one by one


for (i = 0; i <= N - M; i++)
{

// Check the hash values of the current window of text


// and pattern. If the hash values match then only
// Check for characters on by one
if ( p == t )
{
/* Check for characters one by one */
for (j = 0; j < M; j++)
{
if (txt[i+j] != pat[j])
break;
}

// if p == t and pat[0...M-1] = txt[i, i+1, ...i+M-1]


if (j == M)
cout<<"Pattern found at index "<< i<<endl;
}

// Calculate the hash value for next window of text: Remove


// leading digit, add trailing digit
if ( i < N-M )
{
t = (d*(t - txt[i]*h) + txt[i+M])%q;

// We might get a negative value of t, converting it


// to positive
if (t < 0)
t = (t + q);
}
}
}

/* 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 < pattern.size()) {


if (pattern[i] == pattern[length]) {
length++;
lps[i] = length;
i++;
} else {
if (length != 0) {
length = lps[length - 1];
} else {
lps[i] = 0;
i++;
}
}
}
}

// KMP Search function


void KMPSearch(const string& text, const string& pattern) {
int n = text.size();
int m = pattern.size();
vector<int> lps(m);

// Preprocess the pattern


computeLPSArray(pattern, lps);

int i = 0; // Index for text


int j = 0; // Index for pattern

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;
}

You might also like