Check if two strings are permutation of each other in JavaScript
Last Updated :
11 Sep, 2023
In this approach, we are going to discuss how we can check if two strings are permutations of each other or not using JavaScript language. If two strings have the same number of characters rather than having the same position or not it will be a permutation.
Example:
Input: "pqrs" , "rpqs"
Output: True
Explanation: Bothe strings have a same number of characters.
These are the approaches by using these we can Check if two strings are permutations of each other or not:
- Using Sorting
- Using the count the characters method
- Using Map
Method 1: Using Sorting
- Creating a function and storing the length of strings in separate variables.
- Checking if the length of both strings is not the same then we will return false. As strings are not permutations of each other.
- If the length is the same then we will sort both the strings.
- We will check the presence of the same character in both strings in a for loop. If any of the characters get mismatched then we will return false. else we will return true at the end of the for loop.
Example:
Javascript
function arePermutation(str1, str2) {
let n1 = str1.length;
let n2 = str2.length;
if (n1 !== n2)
return false ;
let ch1 = str1.split( '' );
let ch2 = str2.split( '' );
ch1.sort();
ch2.sort();
for (let i = 0; i < n1; i++)
if (ch1[i] !== ch2[i])
return false ;
return true ;
}
let str1 = "test" ;
let str2 = "teat" ;
if (arePermutation(str1, str2))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(nLogn)
Auxiliary space: O(1)
Method 2: Count characters
- Creating a counting array of size 256. In which we are counting the number of characters present in both of the strings in two different array.
- If length of the strings are not same we will return false.
- We are using for loop for comparing the count of character if the count is not same then we will return false else a the end of for loop it will return true.
Example:
Javascript
let NO_OF_CHARS = 256;
function arePermutation(str1, str2) {
let count1 = Array(NO_OF_CHARS).fill(0);
let count2 = Array(NO_OF_CHARS).fill(0);
if (str1.length !== str2.length)
return false ;
for (let i = 0; i < str1.length && i < str2.length; i++) {
count1[str1[i].charCodeAt(0)]++;
count2[str2[i].charCodeAt(0)]++;
}
for (let i = 0; i < NO_OF_CHARS; i++) {
if (count1[i] !== count2[i])
return false ;
}
return true ;
}
let str1 = "geeks" ;
let str2 = "geeks" ;
if (arePermutation(str1, str2))
console.log( "Yes" );
else
console.log( "No" );
|
Time Complexity: O(n)
Auxiliary space: O(n).
Method 3: Using Map
- First we check if the lengths of both strings are equal. If not, they cannot be permutations, so we return false.
- Then we create a Map to store the character frequencies of str1 then, we iterate through str2 and decrement the frequencies in the Map.
- If a character is not found in the Map or its frequency becomes zero, we return false.
- If all characters in str2 are successfully matched, the Map will be empty, and we return true.
Example:
Javascript
function arePermutations(str1, str2) {
if (str1.length !== str2.length) {
return false ;
}
const charFrequencyMap = new Map();
for (let char of str1) {
charFrequencyMap.set(char,
(charFrequencyMap.get(char) || 0) + 1);
}
for (let char of str2) {
if (!charFrequencyMap.has(char)) {
return false ;
}
charFrequencyMap.set(char,
charFrequencyMap.get(char) - 1);
if (charFrequencyMap.get(char) === 0) {
charFrequencyMap. delete (char);
}
}
return charFrequencyMap.size === 0;
}
console.log(arePermutations( "abc" , "cba" ));
|
Time complexity: O(n)
Space Complexity: O(n)
Share your thoughts in the comments
Please Login to comment...