Day 5: 100 Days of DSA

Strings Advanced

Welcome to day 5 of 100 days of DSA challenge where in I’ll be solving five string based questions. Checkout my github repository where I post the solutions of these questions. Let’s dive in!🍀

1. To check if two strings are anagrams or not.

This can be solved by comparing each character in both the strings and making sure that the size of both the strings is also same. This means that both the strings contain the same character, and no other character.

Code:

// To check if a string is anagram or not
#include<bits/stdc++.h>
using namespace std;

bool anagram(string s1,string s2){
    sort(s1.begin(),s1.end());
    sort(s2.begin(),s2.end());
    if(s1==s2){
       return true;
    }
    return false;   
}

int main(){
    string s1,s2;
    cout<<"Enter the first string:";
    cin>>s1;
    cout<<"Enter the second string:";
    cin>>s2;
    if(anagram(s1,s2)){
        cout<<"Yes, both are anagrams."<<endl;
    }
    else{
        cout<<"No, these are not anagrams."<<endl;
    }
}

Output:


2. To find the first non-repeating character in a string

The task is to find the first non repeating character in a string, this can be implemented as follows:

Code:

// To find the first non repeating character in a string
#include<iostream>
using namespace std;


char nonRepeating(string s){
    for(char c:s){
        if(s.find(c)==s.rfind(c)){
            return c;
        }
    }
    return '0';
}

int main(){
    string s;
    cout<<"Enter the string:";
    cin>>s;
    char result=nonRepeating(s);
    if(result=='0'){
        cout<<"No non repeating character found."<<endl;
    }
    else{
        cout<<"The first non repeating character is:"<<result<<endl;
    }
}

Output:


3. To find all substrings in a given string

This can be implemented by iterating over the string from i to j, where in we print every string from i to j in the loop.

Code:

// To find all substrings of a string
#include<iostream>  
using namespace std;

void allSubstrings(string s){
    for(int i=0;i<s.length();i++){
        for(int j=i;j<s.length();j++){
            for(int k=i;k<=j;k++){
                cout<<s[k];
            }
            cout<<endl;
        }
    }
}

int main(){
    string s;
    cout<<"Enter the string:";
    cin>>s;
    allSubstrings(s);
}

Output:


4. To replace space with a special character in a string

The task is to replace the spaces in a string with a special character. To ahieve this w e can iterate over the array and when we encounter space we fill it up with a special character. Now here to take input from user we have use getline(cin,s) since cin stops reading the string after it encounters a space.

Code:

// To replace space with a special character in a string
#include<iostream>
using namespace std;

void replaceSpace(string s,char c){
    for(int i=0;i<s.length();i++){
        if(s[i]==' '){
            s[i]=c;
        }
    }
    cout<<s;
}

int main(){
    string s;
    char c;
    cout<<"Enter the string:";
    getline(cin,s);
    cout<<"Enter the special character:";
    cin>>c;
    replaceSpace(s,c);

}

Output:


5. To solve the longest common prefix in a string problem.

Code:

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

string longestCommonPrefix(vector<string>& strs) {
    if (strs.empty()) {
        return "";
    }

    string prefix = strs[0];
    for (int i = 1; i < strs.size(); i++) {
        string s = strs[i];
        string temp = "";
        for (int j = 0; j < prefix.length() && j < s.length(); j++) {
            if (prefix[j] == s[j]) {
                temp += prefix[j];
            } else {
                break;
            }
        }
        prefix = temp;
        if (prefix.empty()) {
            break;
        }
    }

    return prefix;
}

int main() {
    vector<string> strs;
    int n;
    cout << "Enter the number of strings: ";
    cin >> n;
    cin.ignore();
    cout << "Enter the strings:" << endl;
    for (int i = 0; i < n; i++) {
        string s;
        getline(cin, s);
        strs.push_back(s);
    }
    string str = longestCommonPrefix(strs);
    cout << "The longest common prefix is: " << str << endl;
}

Output:


So that’s all for today, these are the string problems that I solved.