Learn
Share Lesson

Palindromes, Permutations, Anagrams

Loading...

What makes strings unique is that there is a limited set of characters (eg. 26 in the English alphabet), and their order and number of times a character appears conveys meaning. For string interview questions, it's very common to test on this through palindromes, permutations, and anagrams. Having an understanding of what these terms mean before going in can help you feel comfortable diving into a problem.

Palindromes

A palindrome is a word, phrase, or sequence of characters that is the same forward as it is backward.

Examples of palindromes include:

pop

level

racecar

Never odd or even // ignore white space and case

To check for a palindrome, we can do the following:

 

Permutations

A permutation is a different ordering of a string's characters.

For example, a 3 letter word will have 6 (3! = 3 * 2 * 1) permutations. Consider the word dev:

1. dev
2. dve
3. evd
4. edv
5. vde
6. ved

If a question asks if strings are permutations of each other, it is asking if they have the same characters in a different order.

Anagrams

An anagram is a word or phrase that when rearranged will create a new word or phrase. For example silent and listen are anagrams of each other.

You may think anagrams and permutations look similar to each other, and you would be right. All anagrams are permutations of each other, but not all permutations are anagrams. By definition, an anagram must have meaning (be a real word or phrase), but a permutation can be any ordering of the characters without needing to have meaning.

However, interviews often use the terms anagram and permutation interchangeably, and if the question asks for anagrams, it may not specifically require that the words have meaning.

Ready for the next step?
Find a Job
Loading...
Follow teacher
Share:

Table of Contents