Skip to content

Instantly share code, notes, and snippets.

@Robogeek95
Created November 21, 2023 09:42
Show Gist options
  • Save Robogeek95/5714fe91d8ad60d284bd87b0d79ba5d6 to your computer and use it in GitHub Desktop.
Save Robogeek95/5714fe91d8ad60d284bd87b0d79ba5d6 to your computer and use it in GitHub Desktop.
Algorithm to check if two strings are anagrams
function validAnagram(sa, sb) {
if (sa.length !== sb.length) {
return false;
}
const charCount = {};
for (let i = 0; i < sa.length; i++) {
charCount[sa[i]] = (charCount[sa[i]] || 0) + 1;
charCount[sb[i]] = (charCount[sb[i]] || 0) - 1;
if (charCount[sa[i]] < 0 || charCount[sb[i]] > 0) {
return false;
}
}
return true;
}
@Robogeek95
Copy link
Author

This algorithm determines whether two given strings, sa and sb, are valid anagrams of each other. An anagram is a word or phrase formed by rearranging the letters of another.

The algorithm uses a character count approach to efficiently check whether two strings are anagrams by comparing the counts of characters in both strings. If the lengths are different or if the character counts deviate during the iteration, the function returns false; otherwise, it returns true.

Explanation of the algorithm:

if (sa.length !== sb.length) {
    return false;
}

The first step is to check if the lengths of the two input strings are equal. If they are not, the function returns false because strings of different lengths cannot be anagrams.

Character Count Object:

const charCount = {};

The algorithm uses an object called charCount to keep track of the count of each character in the strings.

Character Counting Loop:

for (let i = 0; i < sa.length; i++) {
    charCount[sa[i]] = (charCount[sa[i]] || 0) + 1;
    charCount[sb[i]] = (charCount[sb[i]] || 0) - 1;
...

The algorithm iterates through each character in the input strings using the variable i. For each character in sa, it increments the count in the charCount object, and for each character in sb, it decrements the count.

The expression (charCount[sa[i]] || 0) is used to handle the case when the character has not been encountered yet. If it's undefined (or falsy), it defaults to 0 (initializes it).

Check for Anagram Conditions:

if (charCount[sa[i]] < 0 || charCount[sb[i]] > 0) {
    return false;
}

Inside the loop, it checks if the count of a character in either string becomes negative or positive. If at any point the count becomes negative for a character in sa or positive for a character in sb, the strings are not anagrams, and the function returns false.

Return Result:

return true;

If the loop completes without returning false, it means that both strings have the same characters with the same counts, and the function returns true, indicating that the strings are valid anagrams.

  • Early Exit: Immediately return false if the lengths of the two strings are different, as they cannot be anagrams if the lengths are not equal.
  • Single Loop: Combines the two loops that build the character frequency maps for both strings into a single loop. This approach eliminates the need for a separate loop to compare the character frequencies after building the maps, resulting in better efficiency.
  • Reduce Memory Usage: Instead of creating two separate objects to build the frequency counter, it uses a single object and updates the counts for characters in one pass for string sa and then decrements the counts for string sb. If at any point a count becomes negative, it returns false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment