0% found this document useful (0 votes)
18 views12 pages

STRINGS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

STRINGS

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

class Ssssssssolution {

public:

string removeOuterParentheses(string s) {

int n=s.length();

string res;

int balance=0;

for(int i=0;i<n;i++){

if(s[i]=='('){

if(balance>0){

res+=s[i];

balance++;

}else{

balance--;

if(balance>0){

res+=s[i];

return res;

};
string res="";

for(int j=words.size()-1;j>=0;j--){

res+=s.substr(words[j].first,words[j].second-
words[j].first+1);

if(j!=0){

res+=' ';

return res;

};

class Solution {

public:

string reverseWords(string s) {

int n=s.length();

vector<pair<int,int>>words;

int i=0;

while(i<n){

while(i<n &&s[i]==' ') i++;

if(i==n) break;

int start=i;

while(i<n && s[i]!=' '){

i++;

int end=i-1;

words.push_back({start,end});

}
class Solution {

public:

string largestOddNumber(string num) {

int n=num.length();

for(int i=n-1;i>=0;i--){

if(num[i]%2!=0){

return num.substr(0,i+1);

return "";

};

14)

class Solution {

public:

string longestCommonPrefix(vector<string>&
strs) {

sort(strs.begin(),strs.end());

string s1=strs[0];

int n=strs.size()-1;

string s2=strs[n];

int count=0;

for(int i=0;i<min(s1.length(),s2.length());i+
+){

if(s1[i]==s2[i]){

count++;

}else{

break;

return s1.substr(0,count);

}};
class Solution {

class Solution { public:

public: bool rotateString(string s, string goal) {

bool isIsomorphic(string s, string t) { int n=s.length();

if(s.length()!=t.length()){ if(s.length()!=goal.length()){

return false; return false;

} } // s.substr(1) is a function call on the string s that returns a substring of s, starting from
index 1 and continuing to the end of the string.

int n=s.length();
for(int i=0;i<n;i++){
vector<char>indexs(200,0);
string rotated=s.substr(1)+s[0];
vector<char>indext(200,0);
if(rotated==goal){
for(int i=0;i<n;i++){
return true;
if(indexs[s[i]]!=indext[t[i]]){
}
return false;
s=rotated;
}else{
}
indexs[s[i]]=i+1;
return false;
indext[t[i]]=i+1;

}
}
}
};
return true;

};
class Solution { class Solution {

public: public:

bool isAnagram(string s, string t) { string frequencySort(string s) {

vector<int>sf(26,0); unordered_map<char,int>mp;

vector<int>tf(26,0); for(char &c:s){

if(s.length()!=t.length()) return false; mp[c]++;

int n=s.length(); }

for(int i=0;i<n;i++){ sort(s.begin(),s.end(),[&](char a,char b){

sf[s[i]-'a']++; if(mp[a]==mp[b]){

tf[t[i]-'a']++; return a<b;

}else{

} return mp[a]>mp[b];

for(int i=0;i<26;i++){ }

if(sf[i]!=tf[i]){ });

return false;

} return s;

return true; }

} };

};
class Solution { class Solution {

public: public:

string frequencySort(string s) { string frequencySort(string s) {

map<char, int> m; string ans="";

for (int i = 0; i < s.length(); i++) { unordered_map<char,int>m;

m[s[i]]++;

} for(auto i:s){

m[i]++;

vector<pair<int, char>> v; }

for (auto it : m) { priority_queue<pair<int,char>>p;

v.push_back({it.second, it.first}); for(auto i:m){

} p.push({i.second,i.first});

sort(v.begin(), v.end(), greater<pair<int, while(!p.empty()){


char>>());
int n=p.top().first;

while(n--){
string result = "";
ans+=p.top().second;
for (auto it : v) {
}

p.pop();
for (int i = 0; i < it.first; i++) {
}
result += it.second;
return ans;
}
}
}
};
return result;

};
class Solution {

public:

int maxDepth(string s) {

stack <char> st;

int m=0,ans=0;

for(int i=0;i<s.length();i++){

char c=s[i];

if(c=='('){
class Solution {
st.push(c);
public:
ans++;
int maxDepth(string s) {
}
int maxi=0;
//cout<<ans;
int c=0;
char top=st.top();
int n=s.length();
if(c==')' && top=='('){
for(int i=0;i<n;i++){
st.pop();
if(s[i]=='('){

c++;
m=max(ans,m);
maxi=max(c,maxi);
ans--;

}
}else if(s[i]==')'){
}
c--;
return m;
}else{
}
continue;
};
}

return maxi;

};
class Solution {

public:

int romanToInt(string s) {

unordered_map<char, int> m;

m['I'] = 1;

m['V'] = 5;

m['X'] = 10;

m['L'] = 50;

m['C'] = 100;

m['D'] = 500;

m['M'] = 1000;

int ans = 0;

for(int i = 0; i < s.length(); i++){

if(m[s[i]] < m[s[i+1]]){

ans -= m[s[i]];

else{

ans += m[s[i]];

return ans;

};
neg=true;

continue; }

if(c=='+'){

continue;}

if(c=='0'){

continue;}

if(!isdigit(c)){

return 0;}}

if(isdigit(c)){

res+=c;

continue; }

break; }

if(res == ""){

return 0;}

if

try{ return stoi(res) * -1;}

catch (...){

return INT_MIN;
class Solution { }}
public:

int myAtoi(string s) { try{


bool neg=false; return stoi(res);
bool read=false; }
string res=""; catch (...){
for(char &c:s){ return INT_MAX;
if(!read){ }
if(c==' '){

continue;

read=true; }
if(c=='-'){ };
}

private:

int expandAroundCenter(const string& s, int


left, int right) {

while (left >= 0 && right < s.size() &&


s[left] == s[right]) {

left--;

right++;
class Solution {
}
public:
// Return the length of the palindrome
string longestPalindrome(string s) { found
int n = s.size(); return right - left - 1;
int start = 0, maxLen = 0; }

};
for (int i = 0; i < n; i++) {

// Check for odd length palindromes


centered at i

int len1 = expandAroundCenter(s, i, i);

// Check for even length palindromes


centered between i and i+1

int len2 = expandAroundCenter(s, i, i +


1);

int len = max(len1, len2);

if (len > maxLen) {

maxLen = len;

// Calculate starting index based on


palindrome length

start = i - (len - 1) / 2;

return s.substr(start, maxLen);


class Solution { class Solution {

public: public:

int beautySum(string s) { string reverseWords(string s) {

int sum=0; int n=s.length();

string res; vector<pair<int,int>>words;

int n=s.length(); int i=0;

for(int i=0;i<n;i++){ while(i<n){

map<char,int>mp; while(i<n &&s[i]==' ') i++;

for(int j=i;j<n;j++){ if(i==n) break;

mp[s[j]]++; int start=i;

int mostfreq=INT_MIN; while(i<n && s[i]!=' '){

int leastfreq=INT_MAX; i++;

for(auto it:mp){ }

int end=i-1;
mostfreq=max(mostfreq,it.second); words.push_back({start,end});
leastfreq=min(leastfreq,it.second); }

string res="";
} for(int j=words.size()-1;j>=0;j--){
sum+=mostfreq-leastfreq;
res+=s.substr(words[j].first,words[j].second-
words[j].first+1);
}} return sum;}};
if(j!=0){

res+=' ';

return res; }};

approach 2:

permutations of a string

You might also like