# String

{% hint style="success" %}
**Upper Char to Lower**

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

{% endhint %}

{% hint style="success" %}
**Lower Char to Upper**

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

{% endhint %}

{% hint style="success" %}
How to get the ASC-II value of any char?

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

{% endhint %}

### **`Q1:`**  [Reverse String](https://leetcode.com/problems/reverse-string/)

```cpp
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)**](https://www.geeksforgeeks.org/stdreverse-in-c/)**, and** the space complexity of this program is also **O(1)**.

## `Q2:`  [Palindrome Number](https://leetcode.com/problems/palindrome-number/)

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

## **`Q3:`**  [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)

{% code overflow="wrap" %}

```cpp
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");
}
```

{% endcode %}

## `Q4:` [Maximum Occurring Character](https://practice.geeksforgeeks.org/problems/maximum-occuring-character-1587115620/1)

{% code overflow="wrap" %}

```cpp
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);
```

{% endcode %}

## `Q5:` [Replace Spaces](https://www.codingninjas.com/studio/problems/replace-spaces_1172172?leftPanelTab=0)

{% code overflow="wrap" %}

```cpp
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);
```

{% endcode %}

## `Q6:` [ Remove All Occurrences of a Substring](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/description/)

{% code overflow="wrap" %}

```cpp
#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;
    
}
```

{% endcode %}

## `Q6:`    [Permutation in String](https://leetcode.com/problems/permutation-in-string/)

{% code overflow="wrap" lineNumbers="true" %}

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

{% endcode %}

<details>

<summary>Dry Run</summary>

<img src="https://3848643327-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F07O01HTuDHqLK3IxhYPe%2Fuploads%2FU7SszRgTRiZw2GhI3pxp%2FPermutation.jpg?alt=media&#x26;token=28baea5f-e986-4ba0-9320-cdc08a64d1fa" alt="" data-size="original">

</details>

## Q7:   [Remove all adjacent-duplicates-in-string](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/)

```cpp
#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;
}
```

## Q8: [Removing Stars From a String](https://leetcode.com/problems/removing-stars-from-a-string/)

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