< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page
  • Q1: Reverse String
  • Q2: Palindrome Number
  • Q3: Valid Palindrome
  • Q4: Maximum Occurring Character
  • Q5: Replace Spaces
  • Q6: Remove All Occurrences of a Substring
  • Q6: Permutation in String
  • Q7: Remove all adjacent-duplicates-in-string
  • Q8: Removing Stars From a String

Was this helpful?

String

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

PreviousTwo-PointerNextLeetCode 75

Last updated 1 year ago

Was this helpful?

Upper Char to Lower

    char ch = 'F';
    char res = ch - 'A' + 'a'; // ASC-II value returned 

Lower Char to Upper

    char ch = 'f';
    char res = ch - 'a' + 'A';

How to get the ASC-II value of any char?

    char ch = '1'; // 'a', 'A'....'z'
    int ascii = ch;
    cout<<ascii ;

Q1:

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. , and the space complexity of this program is also O(1).

Q2:

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

Q3:

Q4:

Q5:

Q6:

Q6:

Q7:

Q8:

Reverse String
The time complexity of this function is O(n)
Palindrome Number
Valid Palindrome
Maximum Occurring Character
Replace Spaces
Remove All Occurrences of a Substring
Permutation in String
Remove all adjacent-duplicates-in-string
Removing Stars From a String
Page cover image