Professional Documents
Culture Documents
STRING-1
STRING-1
#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
Roman to integer
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)
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;
}
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;
}
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;
}