Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Permutation

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

bool CheckPermutation(string str1, string str2);

int main()
{
   cout << "Hello World" << endl;
   string str1 = "abcdef";
   string str2 = "cfaedd";
   
  std::cout << CheckPermutation(str1, str2);
   
   return 0;
}


bool CheckPermutation(string str1, string str2) {
    if (str1.length() != str2.length()) {
        return false;
    }
    
    //std::sort(str1.begin(), str1.end());
    //std::sort(str2.begin(), str2.end());
    bool[] letters = new  bool[128];
    
    for (int i=0; i< str1.length(); i++) {
        int val = str1[i];
        letters[val]++;
    }
    
    for(int j=0; j < str2.length(); j++) {
        int val = str2[j];
        letters[val]--;
        
        if(letters[val] < 0) {
            return false;
        }
    }
    
    return true;
    
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.