Open In App

Check if two strings are permutation of each other in JavaScript

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:

Method 1: Using Sorting

Example:






function arePermutation(str1, str2) {
    // Get lengths of both strings
    let n1 = str1.length;
    let n2 = str2.length;
 
    // If length of both strings
    // is not the same, then they
    // cannot be permutations
    if (n1 !== n2)
        return false;
 
    // Convert the strings to arrays
    let ch1 = str1.split('');
    let ch2 = str2.split('');
 
    // Sort both arrays
    ch1.sort();
    ch2.sort();
 
    // Compare sorted arrays
    for (let i = 0; i < n1; i++)
        if (ch1[i] !== ch2[i])
            return false;
 
    return true;
}
 
// Driver Code
let str1 = "test";
let str2 = "teat";
if (arePermutation(str1, str2))
    console.log("Yes");
else
    console.log("No");

Output
No

Time Complexity: O(nLogn)

Auxiliary space: O(1)

Method 2: Count characters

Example:




let NO_OF_CHARS = 256;
 
function arePermutation(str1, str2) {
    // Create 2 count arrays
    // and initialize all values as 0
    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)]++;
    }
 
    // Compare count arrays
    for (let i = 0; i < NO_OF_CHARS; i++) {
        if (count1[i] !== count2[i])
            return false;
    }
 
    return true;
}
 
// Driver code
let str1 = "geeks";
let str2 = "geeks";
 
if (arePermutation(str1, str2))
    console.log("Yes");
else
    console.log("No");

Output
Yes

Time Complexity: O(n)

Auxiliary space: O(n).

Method 3: Using Map

Example:




function arePermutations(str1, str2) {
    // Check if the lengths of
    // both strings are the same
    if (str1.length !== str2.length) {
        return false;
    }
 
    // Create a Map to store character
    const charFrequencyMap = new Map();
 
    // Fill the Map with characters
    for (let char of str1) {
        charFrequencyMap.set(char,
        (charFrequencyMap.get(char) || 0) + 1);
    }
 
    // Iterate through str2
    for (let char of str2) {
        if (!charFrequencyMap.has(char)) {
        // Character not found in str1
            return false;
        }
        charFrequencyMap.set(char,
        charFrequencyMap.get(char) - 1);
        if (charFrequencyMap.get(char) === 0) {
            charFrequencyMap.delete(char);
        }
    }
 
    // If the Map is empty
    // all characters in str2
    //have been matched
    return charFrequencyMap.size === 0;
}
 
// Test cases
console.log(arePermutations("abc", "cba"));

Output
true

Time complexity: O(n)

Space Complexity: O(n)


Article Tags :