Check Permutation: Given two strings, write a method to decide if one is a permutation of the other.
I’m working through algorithm exercises with a group of people, and there was a lot of confusion about what permutation means, and how it differs from anagrams and palindromes.
So, to clarify:
A permutation is one of several possible variations, in which a set of things (like numbers, characters or items in an array) can be ordered or arranged. A permutation of characters does not have to have meaning.
Example: Given the string abcd
, the permutations are abcd
, abdc
, acbd
, acdb
, adbc
, adcb
, bacd
, badc
, bcad
, bcda
, bdac
, bdca
, cabd
, cadb
, cbad
, cbda
, cdab
, cdba
, dabc
, dacb
, dbac
, dbca
, dcab
and dcba
An anagram is a word, phrase, or name formed by rearranging the characters of a string. An anagram must have meaning, it can’t just be gibberish.
Example: These words are anagrams of carets
: caters
, caster
, crates
, reacts
, recast
, traces
A palindrome is a word, phrase, or sequence that reads the same backward as forward. A palindrome must have meaning, it can’t just be gibberish.
Example: Civic
, level
, madam
, mom
and noon
are all palindromes.
All palindromes and anagrams are permutations, but not all permutations are either anagrams or palindromes.
2 Comments
Leave a reply →