Answered step by step
Verified Expert Solution
Question
1 Approved Answer
(Anagrams) Here's a different kind of situation in which recursion provides a neat solution to a problem. Suppose you want to list all the anagrams
(Anagrams) Here's a different kind of situation in which recursion provides a neat solution to a problem. Suppose you want to list all the anagrams of a specified word; that is, all possible letter combinations (whether they make a real English word or not that can be made from the letters of the original word. We'll call this anagramming a word. Anagrainming cat, for example, would produce cal cla CE ica RO Try anagramming some words yourself. You'll find that the number of possibilities is the factonal of the number of letters. For 3 letters there are 6 possible wonds, for 4 letters there are 24 words, for 5 letters 120 words, and so on. (This assumes that all letters are distinct; if there are multiple instances of the same letter, there will be fewer possible words.) How would you write a program to anagram a word? Here's one approach. Assume the word has n letters 1. Anagram the rightmost n-1 letters. Rotale all n lellers 3. Repeat these steps n times To rotate the word means to shift all the lellers one position left, except for the leftmost letter, which "rotates" back to the right, as shown in Figure below. Rotating the word n times gives cach letter a chance to begin the word. While the selected letter occupies this first position, all the other letters are then anagrammed (arranged in every possible position). For cat, which has only 3 letter, rotating the remaining 2 letters simply switches them
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started