Find an hidden permutation of a string C++











up vote
-1
down vote

favorite












I have two strings, and wanted to check if the second is a permutation of the first (and viceversa of course).
So I found out on cplusplus reference that the is_permutation function of the library algorithm could help me. Indeed, I have the following code:



int main () {
string s1 = "bear";
string s2 = "reab";
if ( is_permutation (s1.begin(), s1.end(), s2.begin()) )
cout << "Found permutation.n";
else cout << "No permutations found.n";
return 0;
}


And this works. But now, for example, let's say I still have the string "bear", and a second random string that inside has a permutation of bear, so something like this:



s1 = "bear";
s2 = "AsdVYTcKIyqbNQreabJUoBn";


As you can see there's still the permutation "reab". How can I actually check if there's an hidden permutation? And eventually, save it on a "s3" different string?



Hope you can help me.










share|improve this question


















  • 1




    You can check all four (s1. length) char consecutive substrings and use the is_permutation on each substting.
    – kingW3
    Nov 10 at 10:41

















up vote
-1
down vote

favorite












I have two strings, and wanted to check if the second is a permutation of the first (and viceversa of course).
So I found out on cplusplus reference that the is_permutation function of the library algorithm could help me. Indeed, I have the following code:



int main () {
string s1 = "bear";
string s2 = "reab";
if ( is_permutation (s1.begin(), s1.end(), s2.begin()) )
cout << "Found permutation.n";
else cout << "No permutations found.n";
return 0;
}


And this works. But now, for example, let's say I still have the string "bear", and a second random string that inside has a permutation of bear, so something like this:



s1 = "bear";
s2 = "AsdVYTcKIyqbNQreabJUoBn";


As you can see there's still the permutation "reab". How can I actually check if there's an hidden permutation? And eventually, save it on a "s3" different string?



Hope you can help me.










share|improve this question


















  • 1




    You can check all four (s1. length) char consecutive substrings and use the is_permutation on each substting.
    – kingW3
    Nov 10 at 10:41















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have two strings, and wanted to check if the second is a permutation of the first (and viceversa of course).
So I found out on cplusplus reference that the is_permutation function of the library algorithm could help me. Indeed, I have the following code:



int main () {
string s1 = "bear";
string s2 = "reab";
if ( is_permutation (s1.begin(), s1.end(), s2.begin()) )
cout << "Found permutation.n";
else cout << "No permutations found.n";
return 0;
}


And this works. But now, for example, let's say I still have the string "bear", and a second random string that inside has a permutation of bear, so something like this:



s1 = "bear";
s2 = "AsdVYTcKIyqbNQreabJUoBn";


As you can see there's still the permutation "reab". How can I actually check if there's an hidden permutation? And eventually, save it on a "s3" different string?



Hope you can help me.










share|improve this question













I have two strings, and wanted to check if the second is a permutation of the first (and viceversa of course).
So I found out on cplusplus reference that the is_permutation function of the library algorithm could help me. Indeed, I have the following code:



int main () {
string s1 = "bear";
string s2 = "reab";
if ( is_permutation (s1.begin(), s1.end(), s2.begin()) )
cout << "Found permutation.n";
else cout << "No permutations found.n";
return 0;
}


And this works. But now, for example, let's say I still have the string "bear", and a second random string that inside has a permutation of bear, so something like this:



s1 = "bear";
s2 = "AsdVYTcKIyqbNQreabJUoBn";


As you can see there's still the permutation "reab". How can I actually check if there's an hidden permutation? And eventually, save it on a "s3" different string?



Hope you can help me.







c++ string permutation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 10:36









Froooo

81




81








  • 1




    You can check all four (s1. length) char consecutive substrings and use the is_permutation on each substting.
    – kingW3
    Nov 10 at 10:41
















  • 1




    You can check all four (s1. length) char consecutive substrings and use the is_permutation on each substting.
    – kingW3
    Nov 10 at 10:41










1




1




You can check all four (s1. length) char consecutive substrings and use the is_permutation on each substting.
– kingW3
Nov 10 at 10:41






You can check all four (s1. length) char consecutive substrings and use the is_permutation on each substting.
– kingW3
Nov 10 at 10:41














2 Answers
2






active

oldest

votes

















up vote
0
down vote



accepted










You can use a combination of std::string::substr and is_permutation to achieve this.



// Example program
#include <iostream>
#include <string>
#include <algorithm>
using std::string;
using std::cout;

int main () {
string s1 = "bear";
string s2 = "AsdVYTcKIyqbNQJUoBnreab";
size_t i;
for( i = 0; i <= s2.size() - s1.size(); i++)
{
string s3 = s2.substr(i, s1.size());
if ( is_permutation (s1.begin(), s1.end(), s3.begin()))
{
cout << "Found permutation.n";
break;
}
else
{
continue;
}
}
if(i > s2.size() - s1.size())
cout << "No permutations found.n";
return 0;
}


See live demo here.






share|improve this answer























  • You are welcome :) I made a slight change to the code to include the corner case when the permutation appears at the end of the string as well.
    – P.W
    Nov 10 at 11:28


















up vote
0
down vote













as kingW3 already pointed out in the comments on how one might do it.



string s1 = "bear";
string s2 = "AsdVYTcKIyqbNQreabJUoBn";
string key = "";

for (int i = 0; i < s2.length()+1 - s1.length(); i++)
{
key = s2.substr(i, s1.length());
if (is_permutation(s1.begin(), s1.end(), key.begin()))
cout << "Found permutation.n";
else cout << "No permutations found.n";
}
return 0;


Edit: Please note that the for loop condition has to be writtenm with either +1 or -1 in order to get the last character from your second string.



    i < s2.length()+1 - s1.length()


or



    i < s2.length() - (s1.length()-1)


hope it helps.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238093%2ffind-an-hidden-permutation-of-a-string-c%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    You can use a combination of std::string::substr and is_permutation to achieve this.



    // Example program
    #include <iostream>
    #include <string>
    #include <algorithm>
    using std::string;
    using std::cout;

    int main () {
    string s1 = "bear";
    string s2 = "AsdVYTcKIyqbNQJUoBnreab";
    size_t i;
    for( i = 0; i <= s2.size() - s1.size(); i++)
    {
    string s3 = s2.substr(i, s1.size());
    if ( is_permutation (s1.begin(), s1.end(), s3.begin()))
    {
    cout << "Found permutation.n";
    break;
    }
    else
    {
    continue;
    }
    }
    if(i > s2.size() - s1.size())
    cout << "No permutations found.n";
    return 0;
    }


    See live demo here.






    share|improve this answer























    • You are welcome :) I made a slight change to the code to include the corner case when the permutation appears at the end of the string as well.
      – P.W
      Nov 10 at 11:28















    up vote
    0
    down vote



    accepted










    You can use a combination of std::string::substr and is_permutation to achieve this.



    // Example program
    #include <iostream>
    #include <string>
    #include <algorithm>
    using std::string;
    using std::cout;

    int main () {
    string s1 = "bear";
    string s2 = "AsdVYTcKIyqbNQJUoBnreab";
    size_t i;
    for( i = 0; i <= s2.size() - s1.size(); i++)
    {
    string s3 = s2.substr(i, s1.size());
    if ( is_permutation (s1.begin(), s1.end(), s3.begin()))
    {
    cout << "Found permutation.n";
    break;
    }
    else
    {
    continue;
    }
    }
    if(i > s2.size() - s1.size())
    cout << "No permutations found.n";
    return 0;
    }


    See live demo here.






    share|improve this answer























    • You are welcome :) I made a slight change to the code to include the corner case when the permutation appears at the end of the string as well.
      – P.W
      Nov 10 at 11:28













    up vote
    0
    down vote



    accepted







    up vote
    0
    down vote



    accepted






    You can use a combination of std::string::substr and is_permutation to achieve this.



    // Example program
    #include <iostream>
    #include <string>
    #include <algorithm>
    using std::string;
    using std::cout;

    int main () {
    string s1 = "bear";
    string s2 = "AsdVYTcKIyqbNQJUoBnreab";
    size_t i;
    for( i = 0; i <= s2.size() - s1.size(); i++)
    {
    string s3 = s2.substr(i, s1.size());
    if ( is_permutation (s1.begin(), s1.end(), s3.begin()))
    {
    cout << "Found permutation.n";
    break;
    }
    else
    {
    continue;
    }
    }
    if(i > s2.size() - s1.size())
    cout << "No permutations found.n";
    return 0;
    }


    See live demo here.






    share|improve this answer














    You can use a combination of std::string::substr and is_permutation to achieve this.



    // Example program
    #include <iostream>
    #include <string>
    #include <algorithm>
    using std::string;
    using std::cout;

    int main () {
    string s1 = "bear";
    string s2 = "AsdVYTcKIyqbNQJUoBnreab";
    size_t i;
    for( i = 0; i <= s2.size() - s1.size(); i++)
    {
    string s3 = s2.substr(i, s1.size());
    if ( is_permutation (s1.begin(), s1.end(), s3.begin()))
    {
    cout << "Found permutation.n";
    break;
    }
    else
    {
    continue;
    }
    }
    if(i > s2.size() - s1.size())
    cout << "No permutations found.n";
    return 0;
    }


    See live demo here.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 10 at 11:27

























    answered Nov 10 at 11:00









    P.W

    10.3k2742




    10.3k2742












    • You are welcome :) I made a slight change to the code to include the corner case when the permutation appears at the end of the string as well.
      – P.W
      Nov 10 at 11:28


















    • You are welcome :) I made a slight change to the code to include the corner case when the permutation appears at the end of the string as well.
      – P.W
      Nov 10 at 11:28
















    You are welcome :) I made a slight change to the code to include the corner case when the permutation appears at the end of the string as well.
    – P.W
    Nov 10 at 11:28




    You are welcome :) I made a slight change to the code to include the corner case when the permutation appears at the end of the string as well.
    – P.W
    Nov 10 at 11:28












    up vote
    0
    down vote













    as kingW3 already pointed out in the comments on how one might do it.



    string s1 = "bear";
    string s2 = "AsdVYTcKIyqbNQreabJUoBn";
    string key = "";

    for (int i = 0; i < s2.length()+1 - s1.length(); i++)
    {
    key = s2.substr(i, s1.length());
    if (is_permutation(s1.begin(), s1.end(), key.begin()))
    cout << "Found permutation.n";
    else cout << "No permutations found.n";
    }
    return 0;


    Edit: Please note that the for loop condition has to be writtenm with either +1 or -1 in order to get the last character from your second string.



        i < s2.length()+1 - s1.length()


    or



        i < s2.length() - (s1.length()-1)


    hope it helps.






    share|improve this answer



























      up vote
      0
      down vote













      as kingW3 already pointed out in the comments on how one might do it.



      string s1 = "bear";
      string s2 = "AsdVYTcKIyqbNQreabJUoBn";
      string key = "";

      for (int i = 0; i < s2.length()+1 - s1.length(); i++)
      {
      key = s2.substr(i, s1.length());
      if (is_permutation(s1.begin(), s1.end(), key.begin()))
      cout << "Found permutation.n";
      else cout << "No permutations found.n";
      }
      return 0;


      Edit: Please note that the for loop condition has to be writtenm with either +1 or -1 in order to get the last character from your second string.



          i < s2.length()+1 - s1.length()


      or



          i < s2.length() - (s1.length()-1)


      hope it helps.






      share|improve this answer

























        up vote
        0
        down vote










        up vote
        0
        down vote









        as kingW3 already pointed out in the comments on how one might do it.



        string s1 = "bear";
        string s2 = "AsdVYTcKIyqbNQreabJUoBn";
        string key = "";

        for (int i = 0; i < s2.length()+1 - s1.length(); i++)
        {
        key = s2.substr(i, s1.length());
        if (is_permutation(s1.begin(), s1.end(), key.begin()))
        cout << "Found permutation.n";
        else cout << "No permutations found.n";
        }
        return 0;


        Edit: Please note that the for loop condition has to be writtenm with either +1 or -1 in order to get the last character from your second string.



            i < s2.length()+1 - s1.length()


        or



            i < s2.length() - (s1.length()-1)


        hope it helps.






        share|improve this answer














        as kingW3 already pointed out in the comments on how one might do it.



        string s1 = "bear";
        string s2 = "AsdVYTcKIyqbNQreabJUoBn";
        string key = "";

        for (int i = 0; i < s2.length()+1 - s1.length(); i++)
        {
        key = s2.substr(i, s1.length());
        if (is_permutation(s1.begin(), s1.end(), key.begin()))
        cout << "Found permutation.n";
        else cout << "No permutations found.n";
        }
        return 0;


        Edit: Please note that the for loop condition has to be writtenm with either +1 or -1 in order to get the last character from your second string.



            i < s2.length()+1 - s1.length()


        or



            i < s2.length() - (s1.length()-1)


        hope it helps.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 10 at 11:21

























        answered Nov 10 at 11:01









        ats

        694




        694






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238093%2ffind-an-hidden-permutation-of-a-string-c%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff