Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

STRING

#include<bits/stdc++.h>
Maximum Nesting Depth Of The using namespace std;
Parentheses string reverseString(string &str) {
int maxDepth(string s) int left = 0;
{ int right = str.length() - 1;
int ans =0; string temp = "";
int maxans=INT_MIN; string ans = "";
for(int i=0;i<s.size();i++)
while (left <= right)
{
{
if(s[i]=='(')
{ char ch = str[left];
ans++; if (ch != ' ')
} {
else if(s[i]==')') temp += ch;
{ }
ans--; else if (ch == ' ')
} {
maxans = max(ans,maxans); if (!temp.empty())
}
{
return maxans;
if (!ans.empty())
}
{
ans = temp + " " + ans;
} else
{
ans = temp;
}
temp = "";
}
}
left++;
}
if (!temp.empty())
{
if (!ans.empty())
{
ans = temp + " " + ans;
}
else
{
ans = temp;
}
}
return ans;
}

#include<bits/stdc++.h> #include<bits/stdc++.h>
using namespace std; using namespace std;
string reverseString(string &str) {
string reverseString(string &str) int left = 0;
{ int right = str.length() - 1;
stack<string> st; string temp = "";
string s1 = ""; string ans = "";
for (int i = 0; i < str.size(); i++) while (left <= right)
{ {
if (str[i] == ' ') char ch = str[left];
{ if (ch != ' ')
if (s1.empty()==false) {
{ temp += ch;
st.push(s1); }
s1 = ""; else if (ch == ' ')
} {
} if (!temp.empty())
else {
{ if (!ans.empty())
s1 += str[i]; {
} ans = temp + " " + ans;
} } else
{
if (!s1.empty()) ans = temp;
{ }
st.push(s1); temp = "";
} }
}
string ans = ""; left++;
while (!st.empty()) }
{ if (!temp.empty())
ans += st.top() + " "; {
st.pop(); if (!ans.empty())
} {
if (!ans.empty()) ans = temp + " " + ans;
{ }
ans.pop_back(); else
} {
ans = temp;
return ans; }
} }
return ans;
}
Time Complexity: O(N), Traversing the
entire string
TIME COMPLEXITY = O(N)
Space Complexity: O(N), Stack and ans SPACE COMPLEXITY = O(1)
variable
Find the Rightmost Odd Digit Longest Common Prefix
string commonPrefix(vector<string>& arr,int n)
string largestOddNumber(string num) {
{ string ans="";
string ans = ""; sort(arr.begin(),arr.end());
for (int i = num.size() - 1; i >= 0;i--) string s=arr[0];
{ string e=arr[n-1];
if (num[i] % 2 != 0) for(int i =0; i<arr[0].size();i++)
{ {
for (int j = 0; j <= i; j++) if(s[i]==e[i])
{ {
ans.push_back(num[j]); ans+=s[i];
} }
break; else
} {
} break;
return ans; }
} }
if(ans=="")
{
ans+="-1";
}
return ans;
}
Isomorphic Strings Anagram Pairs
bool areIsomorphic(string &str1, string &str2)
{
map<int,int> mpp;
set<int>st;
if(str1.size()!=str2.size())
{
return 0;
}
for(int i =0;i<str1.size();i++)
{
char char1= str1[i];
char char2 = str2[i];
if(mpp.find(char1) !=mpp.end()) bool isAnagram(string str1, string str2)
{ {
if(mpp[char1]!=char2) int p =str1.size();
{ int q =str2.size();
return false; if(p!=q) return false;
} sort(str1.begin(), str1.end());
} sort(str2.begin(), str2.end());
else for(int i =0;i<p;i++)
{ {
if(st.find(char2)!=st.end()) if(str1[i]!=str2[i]) return false;
{ }
return false; return true;
} }
else
{ Time Complexity: O(nlogn) since sorting
mpp[char1]=char2; function requires nlogn iterations.
st.insert(char2);
} Space Complexity: O(1)
}
}
return true;
}
Check If One String Is A Rotation Of Another String

Check If One String Is A Rotation Of Sorting Characters By Frequency


Another String string sortByFrequency(int n, string& s)
{
int isCyclicRotation(string &p, string &q) vector<string>str(n+1,"");
{ unordered_map<char,int>mp;
string res=p; for(auto x:s)
int j = p.length()-1; {
do{
mp[x]++;
string temp = "";
}
temp = temp +
res.substr(j)+res.substr(0,j); for(auto it:mp)
if(temp==q) {
{ str[it.second].append(it.second,it.first);
return 1; }
} string res="";
res=temp; for (int i=0;i<str.size();i++)
j=res.length()-1; {
} while(res!=p); res+=str[i];
return 0; }
} reverse(res.begin(),res.end());
return res;
}
bool areRotations(string s1,string s2)
{
if(s1.length()!=s2.length())
{
return false;
}
s1+=s1;
return s1.find(s2)!=string::npos;
}

Roman to integer

Count with k different characters


int countSubStrings(string str, int k) int countSubStrings(string s, int k)
{ int find = 0; {
for (int i = 0; i < str.size(); i++) int ans=0;
{ int cnt = 0; for(int i=0,j=k-1;j<s.size();i++,j++)
int charcount[26] = {0}; {
for (int j = i; j < str.size(); j++) { vector<int> v(26,0);
int charindex = str[j] - 'a'; int count=0;
if (charcount[charindex] == 0) for(int l=i;l<s.size();l++)
{ {
charcount[charindex]++; if(v[s[l]-'a']==0)
cnt++; {
} count++;
if (cnt == k) }
find++; v[s[l]-'a']++;
else if (cnt > k) cnt++; if(count==k)
}} {
return find; ans++;
} }
else if(count>k)
{
break;
}
}
}
return ans;
}
int sumOfBeauty(string s) int sumOfBeauty(string s)
{ {
int n = s.size(); int n = s.size();
int sum=0; int sum=0;
for(int i=0;i<n;i++) for(int i=0;i<n;i++)
{ {
map<char,int>mpp; map<char,int>mpp;
for(int j=i;j<n;j++) for(int j=i;j<n;j++)
{ {
int mn= INT_MAX; int mn= INT_MAX;
int mx= INT_MIN; int mx= INT_MIN;
mpp[s[j]]++; mpp[s[j]]++;
for(auto it : mpp) for(auto it : mpp)
{ {
mx=max(mx,it.second); mx=max(mx,it.second);
mn=min(mn,it.second); mn=min(mn,it.second);
} }
sum+=mx-mn; sum+=mx-mn;
} }
} }
return sum; return sum;
} }
Minimum Cost To Make String Valid

int findMinimumCost(string str) int findMinimumCost(string str)


{ {
int n = str.length(); int temp ;
if(n%2!=0) return -1; int res;
stack<char>st; int n = str.size();
for(int i =0;i<n;i++) if(n%2!=0) return -1;
{ for(int i =0;i<n;i++)
if(str[i]=='}' && !st.empty()) {
{ if(str[i]=='{')
if(st.top()== '{') {
{ temp++;
st.pop(); }
} else
else st.push(str[i]); {
} if(temp==0)
else st.push(str[i]); {
} res++;
int red_len = st.size(); temp++;
int n1=0; }
while(!st.empty() && st.top()=='{') else temp--;
{ }
st.pop(); }
n1++; if(temp>0)
} {
return (red_len/2 + n1%2); res+=temp/2;
} }
Time Complexity: O(n) return res;
Auxiliary Space: O(n) }
Time Complexity: O(n)
Auxiliary Space: O(1)

Search Pattern (KMP - Algorithm)


Search Pattern (KMP - Algorithm)
vector<int> stringMatch(string text, vector<int> stringMatch(string
string pattern) text, string pattern)
{ {
vector<int>ans; int n = text.length();
for(int i=0; i<text.size();i++) int m = pattern.length();
{ vector<int>ans, LPS(m, 0);
int j; int len = 0;
for(j=0; j<pattern.size(); j++) for(int i = 1; i < m; i++)
{ {
if(text[i+j]!=pattern[j]) if(pattern[i] ==
{ pattern[len])
break; {
} len++;
} LPS[i] = len;
if(j==pattern.size()) }
{ else
ans.push_back(i+1); {
} if(len != 0)
} {
return ans; len = LPS[len-1];
} }
}
}
● Time Complexity: O(N * M) int i = 0;
● Space Complexity: O(N) int j = 0;
while(i < n){
if(text[i] == pattern[j])
{
i++;
j++;
if(j == m)
{

ans.push_back(i-j+1);
j = LPS[j-1];
}
}

else
{
if(j > 0) j = LPS[j-1];
else i++;
}
}
return ans;
}
Time Complexity: O(N+M) where N is the
length of the text and M is the length of the
pattern to be found.
Auxiliary Space: O(M)

Z -METHOD for searching


vector<int> search(string s, string
pattern)
{
vector<int> ans;
vector<int> z(0);
string new_string=pattern+'#'+s;
int m=pattern.size();
int n=s.size();
int mn = new_string.size();
int i=0;
while(i<mn)
{
if(i<=m)
{
z.push_back(0);
}
else
{
int left=0;
int right =0;

if(new_string[i]==new_string[left])
{
right =i;

while(new_string[left]==new_string[right
] && right<mn)
{
left++;
right++;
}
}
z.push_back(left);
}
i++;
}
for(int i=0;i<mn;i++)
{
if(z[i]==m) ans.push_back(i-m);
}
return ans;
}

Minimum Characters For Palindrome

int # include<bits/stdc++.h>
minCharsforPalindrome(string using namespace std;
str) vector<int> lps (string str)
{ {
int n = str.size(); int n = str.size();
int i =0,c=0; int len=0;
int j=n-1; int i=1;
int k=j; vector<int> LPS(n,0);
while(i<=j) while(i<n)
{ {
if(str[i]==str[j]) if(str[i]==str[len])
{ {
i++; len++;
j--; LPS[i]=len;
} i++;
else }
{ else
i=0; {
k--; if(len!=0)
j=k; {
c++; len =
} LPS[len-1];
} }
return c; else
} {
LPS[i]=0;
t=o i++;
}
}
}
return LPS;
}
int
minCharsforPalindrome(string
str)
{
string rev=str;
reverse(rev.begin(),
rev.end());
string str1=str + '$'+
rev;

vector<int>ans=lps(str1);
int result =
str.size()-ans.back();
return result;
}

time complexity of O(n) and a space


complexity of O(n)

Longest Prefix Which is Suffix

string #include<bits/stdc++.h>
longestPrefixSuffix(string using namespace std;
&str) string longestPrefixSuffix(string
{ &str)
int n = str.size(); {
int i = 1, len = 0; int n = str.size();
while (i < n) string ans = "";
{ int i = 1;
if (str[i] == int len = 0;
str[len]) vector<int> LPS(n, 0);
{ while (i < n) {
len++; if (str[i] == str[len])
i++; {
} len++;
else LPS[i] = len;
{ i++;
if (len != 0) }
{ else
len = 0; {
} if (len != 0)
else {
{ len = LPS[len - 1];
i++; } else
} {
} LPS[i] = 0;
} i++;
return str.substr(0, }
len); }
} }
int cnt = 0;
for (int i = n - 1; i >= 0; i--)
{
if (LPS[i] != 0)
{
cnt++;
}
else
{
break;
}
}
if (cnt <= n)
{
ans = str.substr(0, cnt);
}
return ans;
}

You might also like