Friday, October 10, 2025
HomeData Modelling & AIis_permutation() in C++ and its application for anagram search

is_permutation() in C++ and its application for anagram search

is_permutations() is used to check if two containers like string and vector are permutation of each other. It accepts three parameters, the first two parameters are begin and end positions of first object and third parameter begin position of second object.
 

CPP




// C++ program to demonstrate working of
// is_permutation()
#include <bits/stdc++.h>
using namespace std;
  
// Driver program to test above
int main()
{
    vector<int> v1{1, 2, 3, 4};
    vector<int> v2{2, 3, 1, 4};
  
    // v1 and v2 are permutation of each other
    if (is_permutation(v1.begin(), v1.end(), v2.begin()))
        cout << "True\n";
    else
        cout << "False\n";
  
    // v1 and v3 are NOT permutation of each other
    vector<int> v3{5, 3, 1, 4};
    if (is_permutation(v1.begin(), v1.end(), v3.begin()))
        cout << "True\n";
    else
        cout << "False\n";
  
    return 0;
}


Output : 

True
False

 
Application : 
Given a pattern and a text, find all occurrences of pattern and its anagrams in text.
Examples: 
 

Input : text ="forxxorfxdofr"  
        pat = "for"
Output :  3
There are three anagrams of "for"
int text.

Input : word = "aabaabaa" 
        text = "aaba"
Output : 4

 

We have discussed a (n) solution her. But in this post it is done using is_permutation(). Although the complexity is higher than previously discussed method, but the purpose is to explain application of is_permutation().
Let size of pattern to be searched be pat_len. The idea is to traverse given text and for every window of size pat_len, check if it is a permutation of given pattern or not.
 

CPP




// C++ program to count all permutation of
// given text
#include<bits/stdc++.h>
using namespace std;
  
// Function to count occurrences of anagrams of pat
int countAnagrams(string text, string pat)
{
    int t_len = text.length();
    int p_len = pat.length();
  
    // Start traversing the text
    int count = 0; // Initialize result
    for (int i=0; i<=t_len-p_len; i++)
  
        // Check if substring text[i..i+p_len]
        // is a permutation of pat[].
        // Three parameters are :
        // 1) Beginning position of current window in text
        // 2) End position of current window in text
        // 3) Pattern to be matched with current window
        if (is_permutation(text.begin()+i,
                           text.begin()+i+p_len,
                           pat.begin()))
            count++;
  
    return count;
}
  
// Driver code
int main()
{
    string str = "forxxorfxdofr";
    string pat = "for";
    cout << countAnagrams(str, pat) << endl;
    return 0;
}


Output: 
 

3

If you like neveropen and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the neveropen main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
 

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32349 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6715 POSTS0 COMMENTS
Nicole Veronica
11878 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6837 POSTS0 COMMENTS
Ted Musemwa
7097 POSTS0 COMMENTS
Thapelo Manthata
6792 POSTS0 COMMENTS
Umr Jansen
6791 POSTS0 COMMENTS