How to use XOR Operation In Javascript

Using XOR operation, XOR all characters in both strings. If the result is zero, the strings have the same characters with the same frequency, indicating they are permutations of each other.

Example: In this example The function arePermutations compares lengths, then bitwise XORs character codes of each string. Returns true if the XOR result is 0, indicating permutation

JavaScript
function arePermutations(str1, str2) {
  if (str1.length !== str2.length) {
    return false;
  }

  let result = 0;
  for (let i = 0; i < str1.length; i++) {
    result ^= str1.charCodeAt(i) ^ str2.charCodeAt(i);
  }

  return result === 0;
}

const string1 = "abc";
const string2 = "bca";
console.log(arePermutations(string1, string2));

const string3 = "abc";
const string4 = "def";
console.log(arePermutations(string3, string4));

Output
true
false


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: Both 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:

Table of Content

  • Using Sorting
  • Count characters
  • Using Map
  • Using XOR Operation

Similar Reads

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....

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....

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....

Using XOR Operation

Using XOR operation, XOR all characters in both strings. If the result is zero, the strings have the same characters with the same frequency, indicating they are permutations of each other....