1

This work I got from my teacher I'd love to residents I worked on it for a week Write a recursive method signature it - public void printSubs (String s) - method parameter in string s. The method prints all letters you can build words of s, each length (from one hundred to all the letters) when the order of the letters in words have to be like their arrangement in s. For example, if s = "bye" method prints the following strings: "B", "y", "e", "by", "ye", "be", "bye" If s = "home" system prints the following strings: "H", "o", "m", "e", "ho", "om", "me", "hm", "he", "oe", "hom", "ome", "hme "," hoe "," home "

This is what I wrote:

private String printSubs(String s, String a) { if(s.length()==0) { return a+""; } return printSubs(s.substring(1), a+s.substring(0,1)) + ", " + printSubs(s.substring(1), a); } 

The result:

bye, by, be, b, ye, y, e, 
5
  • 3
    Try to format your post a little bit better than the current one. Commented Jan 18, 2017 at 16:14
  • 2
    What is your question? Commented Jan 18, 2017 at 16:14
  • 3
    Do your homework on your own, lad. You aren't supposed to post homework questions here. Commented Jan 18, 2017 at 16:15
  • There's a rather easy non-recursive way to do it. Is it mandatory to have it to be recursive? Commented Jan 18, 2017 at 16:28
  • So what's the problem? From what you describe, your program already prints the seven (2^n - 1) required strings. Commented Jan 18, 2017 at 17:36

1 Answer 1

1
import java.util.ArrayList; import java.util.List; public class Test20170118 { public static void main(String[] args) { String s = "esempio"; List<String> result = new ArrayList<>(); result.add(""); System.out.println(method(s, result)); System.out.println(method(s, result).size()); } private static List<String> method(String input, List<String> done){ List<String> result = new ArrayList<>(); if(input.length() == 1){ for (String string : done) { result.add(string); result.add(string + input); } return result; } else { for (String string : done) { result.add(string); result.add(string + input.substring(0, 1)); } return method(input.substring(1), result); } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.