logo CodeStepByStep logo

decodeMessage

Language/Type: Java recursion backtracking
Related Links:

Write a recursive method named decodeMessage that accepts a string s consisting only of digit characters, treats the string as an encoded message as described below, and prints all possible that could be made from those characters. For this problem we will use an encoding system where each character of the alphabet is represented by a corresponding integer from 1 to 26:

  • A: 1
  • B: 2
  • C: 3
  • ...
  • Y: 25
  • Z: 26

For example, if the string to decode is "25", it could either represent a '2' (B) followed by a '5' (E), or a "25" (Y). So the call of decodeMessage("25"); should print the following console output:

BE
Y

You may print the possible decodings in any order. You may assume that the string passed contains only digit characters from 0-9.

Constraints: Do not declare any global variables. You can use any data structures you like, and your code can contain loops, but the overall algorithm must be recursive and must use backtracking. You are allowed to define other "helper" methods if you like; they are subject to these same constraints.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.