Pattern visualizer
Valid Anagram
Two strings are anagrams exactly when they contain the same letters the same number of times — order doesn't matter, only each character's frequency. So count every letter in the first string, then walk the second string decrementing those same counts; if the second string never demands a letter it doesn't have and every count lands back at zero, the frequencies matched exactly. Reach for it on any 'same letters, same frequencies' check. Animated on: s="anagram", t="nagaram" — is t an anagram of s?.
Strings
s="anagram" (idx0-6), t="nagaram" (idx7-13). Same length (7=7) — proceed.
1FUNCTION isAnagram(s, t):2 IF length of s != length of t: RETURN false3 count = an empty tally of characters4 FOR each ch in s: add 1 to count for ch5 FOR each ch in t:6 IF count for ch is 0 (or missing): RETURN false7 subtract 1 from count for ch8 END FOR9 RETURN true10END FUNCTION
← / → step · space play · Home restart