Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to check whether two strings contain same characters in same order in C++

1 Answer

0 votes
#include <bits/stdc++.h> 
     
using namespace std; 
    
string remove_duplicate_characters(string s) {
    if (s.begin() == s.end()) return s;
       
    auto no_dup = s.begin(); 
       
    for (auto current = no_dup; current != s.end();) {
        current = find_if(next(current), s.end(), [no_dup](const char ch) {
                          return ch != *no_dup; 
                  });
        *++no_dup = move(*current);;
    }
    s.erase(no_dup, s.end());
       
    return s;
}
   
bool contain_same_characters_and_order(string s1, string s2) {
    string s1_tmp = remove_duplicate_characters(s1);
    string s2_tmp = remove_duplicate_characters(s2);
 
    return s1_tmp == s2_tmp;
}
       
int main() 
{ 
    string s1 = "c++ programming";
    string s2 = "c+++++ ppppprooooogrammingggg";
        
    if (contain_same_characters_and_order(s1, s2))
        cout << "yes";
    else
        cout << "no";
    
    return 0; 
} 
     
     
     
/*
run:
     
yes
      
*/

 



answered Oct 29, 2019 by avibootz
edited Oct 31, 2019 by avibootz

Related questions

...