Check if two strings are permutation of each other | C ++ program to check whether two strings are Permutations of each other ; function to check whether two strings are Permutation of each other ; Get lengths of both strings ; If length of both strings is not same , then they cannot be Permutation ; Sort both strings ; Compare sorted strings ; Driver program to test to print printDups
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool arePermutation ( string str1 , string str2 ) { int n1 = str1 . length ( ) ; int n2 = str2 . length ( ) ; if ( n1 != n2 ) return false ; sort ( str1 . begin ( ) , str1 . end ( ) ) ; sort ( str2 . begin ( ) , str2 . end ( ) ) ; for ( int i = 0 ; i < n1 ; i ++ ) if ( str1 [ i ] != str2 [ i ] ) return false ; return true ; } int main ( ) { string str1 = " test " ; string str2 = " ttew " ; if ( arePermutation ( str1 , str2 ) ) printf ( " Yes " ) ; else printf ( " No " ) ; return 0 ; }