Page cover

String

https://cplusplus.com/reference/string/string/

void reverseString(vector<char>& s) {
        reverse(s.begin(), s.end());
}

It reverses the order of the elements in the range [first, last) of any container. The time complexity of this function is O(n), and the space complexity of this program is also O(1).

bool isPalindrome(int x) {
    string s = to_string(x);
    string temp = to_string(x);

    reverse(s.begin(), s.end());
    if(s == temp)
         return true;
    else
         return false;
}

bool isPalindrome(string s) {
    string result;
    for (char c : s) {
        if (isalnum(c)) {  // if alphanumeric characters then read otherwise skip
            result += c;
        }
    }
    
    for (int i = 0; i < result.length(); i++){
        result[i] = tolower(result[i]);
    }
    
    string temp = result;
    reverse(temp.begin(), temp.end());
    
    if(temp == result){
        return true;
    }
    else{
        return false;
    }
}
int main(){
    cout << isPalindrome("12-/#@%3Abcba321");
}

char getMaxOccuringChar(string str){
    int freq[26] = {0}; //S.C -> O(1)
   
    for(int i=0; i<str.length();i++){  //T.C -> O(N)
        tolower(str[i]);
        
        int index = str[i] -'a'; // 'f' - 'a' = 102 - 97 = 5
        freq[index]++; // [5] = 0++ = 1;
    }
    
    int jump;
    int maxi = INT_MIN;
    
    for(int i=0; i<26; i++){ //T.C -> O(1)
        if(freq[i]>maxi){
            maxi = freq[i];
            jump = i; 
        }
    }
    return 'a' + jump; // 'a' + 5 = 97 + 5 = 102; f is returned
}

// S.C = O(1);
// T.C = O(N);

string replaceSpaces(string str){
    string s = "";
    for(int i = 0; i < str.size(); i++){
        if(str[i] == ' '){
            s.push_back('@');
            s.push_back('4');
            s.push_back('0');
        }
        else{
            s.push_back(str[i]);
        }
    }
    return s;
}
int main(){
    cout << replaceSpaces("i am nafees"); // output -> i@40am@40nafees 
}


// S.C = O(N);
// T.C = O(N);

#include <iostream>
#include <cstring>
using namespace std;

void removeOccurrences(char* s, int n, char* t, int m) {
    for (int i = 0; i < n; i++) {
        if(s[i] == t[0]) {
            int Ithindex = i;
            int count = 0;
            for (int j = 0; j < m; j++) {
                if(s[Ithindex] != t[j]) 
                    break;
                Ithindex++;
                count++;
            }
            if(count == m) {
                int temp = i;
                for (; temp < n; temp++) {
                    s[temp] = s[Ithindex++];
                }   
            }
        }
    }  
}


int main(){
    char s[] = "amssabcmdabc";
    char t[] = "abc";
    removeOccurrences(s, strlen(s), t, strlen(t));
    cout << s;
    
}

class Solution {
private:
bool checkEqual(int *count1, int *count2){
    for(int i = 0; i < 26; i++){
        if(count1[i] != count2[i])
            return false;
    }
    return true;
}
public:
    bool checkInclusion(string s1, string s2) {
        int count1[26] = {0};
        for(int i = 0; i < s1.length(); i++){
            int index = s1[i] - 'a';
            count1[index]++;  
        }
        // travese s2 string in window of size s1 length and compare
        int i = 0;
        int windowSize = s1.length();
        int count2[26] = {0};
        // running for first window
        while(i < windowSize && i < s2.length()){
            int index = s2[i] - 'a';
            count2[index]++;
            i++;
        }

        if(checkEqual(count1, count2))
            return true;
        
        // process remaining window
        while(i < s2.length()){
            char newChar = s2[i];
            int index = newChar - 'a';
            count2[index]++;

            char oldChar = s2[i-windowSize];
            index = oldChar - 'a';
            count2[index]--;

            i++;
            if(checkEqual(count1, count2))
                return true;
        }
        return false;
    }
};
Dry Run

#include <iostream>
#include <cstring>
using namespace std;

void removeOccurrences(char* s, int n, char* t, int m) {
    for (int i = 0; i < n; i++) {
        if(s[i] == t[0]) {
            int Ithindex = i;
            int count = 0;
            for (int j = 0; j < m; j++) {
                if(s[Ithindex] != t[j]) 
                    break;
                Ithindex++;
                count++;
            }
            if(count == m) {
                int temp = i;
                for (; temp < n; temp++) {
                    s[temp] = s[Ithindex++];
                }   
            }
        }
    }  
}


int main(){
    char s[] = "amssabcmdabc";
    char t[] = "abc";
    removeOccurrences(s, strlen(s), t, strlen(t));
    cout << s;
}

class Solution {
public:
     
    string removeStars(string str) {
        stack<char> st;
        for(int i = 0; i < str.size(); i++) {
            if(str[i] != '*') {
                st.push(str[i]);
            }
            if(!st.empty() && str[i] == '*') {
                st.pop();
            }
        }
        string res = "";
        int i = 0;
        while(!st.empty()) {
            res += st.top();
            st.pop();
        }
        int s = 0, e = res.size()-1;
        while(s<=e) {
            swap(res[s++], res[e--]);
        }
        return res;
    }
};

Last updated

Was this helpful?