Creating recursion in Java
Initially it seemed dull, but the more I try to do it, the more I get stuck.
I want to create a recursive method that will accept a combination of strings and dotted results as shown below:
input: test
output:
test
t.est
te.st
tes.t
t.es.t
te.s.t
t.e.s.t
t.e.st
you get the idea ... I need all the dot permutations between characters. But the two dots must not appear together, and the dot must not be the first or last character.
The code I wrote is:
public final class Main{
public static void main(String[] args) throws Exception{
recurse ("test");
}
public static void recurse(String str){
recurse("",str);
}
public static void recurse (String pre, String str){
if(str.length() > 1){
recurse(pre+str.substring(0,1)+".",str.substring(1));
}
System.out.println(pre+str);
}
}
But I cannot get around it. What change should I make?
Just for clarification I got the idea from https://medium.com/@JakeCooper/so-nice-i-did-it-twice-hacking-the-oneplus-reservation-system-again-2e8226c45f9a but I have no intention of hacking invitation systems.
source to share
Try
public static void recurse(String prev, String next) {
if (next.length() == 0)
System.out.println(prev);
else {
recurse(prev + next.charAt(0), next.substring(1));
recurse(prev + "." + next.charAt(0), next.substring(1));
}
}
If you allow the .
entire line to be preceded, use:
String s = "test";
recurse("",s);
If you don't allow .
precedence:
recurse(s.charAt(0), s.substring(1));
If you've added .
(i.e. test.
, t.e.s.t.
), change the if block to
if (next.length() == 0) {
System.out.println(prev);
System.out.println(prev + ".");
}
source to share
I would pass the position in the string to the recurse function:
public static void recurse(String str) {
recurse(str, 1); // start only at 1 since we do not want dot at first position
}
public static void recurse(String str, int n) {
if (n >= str.length()) { // >= since no dot at last position
System.out.println(str); // ok we have a full string : print it
} else {
recurse(str, n + 1); // either not dot and recurse
str = str.substring(0, n) + "." + str.substring(n); // or add one at n
recurse(str, n + 2); // and recurse too
}
}
The output is recurse("test")
as expected:
test
tes.t
te.st
te.s.t
t.est
t.es.t
t.e.st
t.e.s.t
source to share
You can use the following piece of code:
public static void recurse (String pre, String str){
if(str.length() > 1) {
String str1 = pre + str.substring(0,1) + ".";
String str2 = str.substring(1);
System.out.println(pre + str);
recurse(str1, str2);
}
System.out.println(pre + str);
}
Output:
test
t.est
t.e.st
t.e.s.t
t.e.st
t.est
test
source to share
You need to overwrite in a loop to get all the possibilities.
public static void recurse(String pre, String str) {
pre += ".";
System.out.println(pre + str);
for (int i = 1; i <= str.length(); i++)
recurse(pre + str.substring(0, i), str.substring(i));
}
Output:
.tes
.t.es
.t.e.s
.t.e.s.
.t.es.
.te.s
.te.s.
.tes.
Note. You may need to change this value slightly to not include .
at the beginning.
source to share
My guess is that you want to add a new character to pre
for each recursive call, taking the first character from str
, so you should stop when str
there is only one character left in .
public static void recurse(String pre, String str) {
if (str.length() == 1) {
System.out.println(pre + str);
System.out.println(pre + " ." + str);
return;
}
recurse(pre + str.substring(0, 1), str.substring(1));
if (pre.length() > 0)
recurse(pre + "." + str.substring(0, 1), str.substring(1));
}
Calling with recurse("", "test");
gives you:
test
tes .t
te.st
te.s .t
t.est
t.es .t
t.e.st
t.e.s .t
source to share