Help in converting a certain recurtion

  • Thread starter Thread starter transgalactic
  • Start date Start date
Click For Summary
SUMMARY

The discussion focuses on generating mnemonic combinations for phone numbers using recursion in Java. The user seeks to implement a method named listMnemonics that produces all possible letter combinations corresponding to a given string of digits. The provided code demonstrates a recursive approach to generate subsets of characters, but the user struggles with adapting it to handle variable-length combinations based on the digit input. The solution requires refining the recursive logic to accommodate the specific mapping of digits to letters.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with recursion and recursive methods
  • Knowledge of character mapping for phone digits (e.g., 2 = ABC, 3 = DEF)
  • Experience with string manipulation in Java
NEXT STEPS
  • Implement a mapping function to convert digits to corresponding letters
  • Refine the recursive method to generate combinations based on the length of the input string
  • Explore Java's StringBuilder for efficient string concatenation
  • Test the method with various digit inputs to ensure all combinations are generated
USEFUL FOR

Software developers, particularly those working on telecommunication applications, algorithm enthusiasts, and anyone interested in mastering recursion in Java.

transgalactic
Messages
1,386
Reaction score
0
here is a question and i have a very similar code but i need to convert it
some how in order to fit this question

question:
In order to make their phone numbers more memorable, service providers like to find numbers that spell out some word (called a mnemonic) appropriate to their business that makes that phone number easier to remember. For example, the phone number for a recorded time-of-day message in some localities is 637-8687 (NERVOUS). Imagine that you have just been hired by a local telephone company to write a method listMnemonics that will generate all possible letter combinations that correspond to a given number, represented as a string of digits. For example, if you call
recursiveObject.listMnemonics("723")
your method shall generate and print out (using recursion) the following 27 possible letter combinations that correspond to that prefix:
PAD PBD PCD RAD RBD RCD SAD SBD SCD
PAE PBE PCE RAE RBE RCE SAE SBE SCE
PAF PBF PCF RAF RBF RCF SAF SBF SCF

my efforts in solving it:
i know that in order to solve this question is need to use a recursive methos that
prints out all the subsets of a given string letters with a given resolt string size

i have built this code
Code:
ublic class subsets {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String word="abcd";
		
		
subsets(word.length(),3,"",word);
	}
	public static void phone(String phone){
		
	}
	public static void subsets(int n,int k,String also,String word){
		if (n==0){
			if (also.length()==k){
			System.out.println(also);
			}
		}
		else{
			subsets(n-1,k,also+word.charAt(n-1),word);
			subsets(n-1,k,also,word);
			
				
			
		}
	}

}
 
Last edited:
Technology news on Phys.org
i had a theoretical idea the we put an inner counter for each cell
and then count them like numbers
001
002
003
but for example in order to have a combination for a number with length 4
we need to have 4 "for" loops one inside another

for 5 digit number we need to have 5 "for" loop one inside of the other

how do i solve its for every given length
 

Similar threads

Replies
13
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K