java - Performing Recursion inside For loop -
i trying understand code of writing permutations given input string.
for example: input string:123,output:123,132,213,231,312,321.
below pasted code snippet that.
public static void main(string args[]) { permutestring("", "123"); } public static void permutestring(string beginningstring, string endingstring) { if (endingstring.length() <= 1) system.out.println(beginningstring + endingstring); else (int = 0; < endingstring.length(); i++) { try { // system.out.println(i); string newstring = endingstring.substring(0, i) + endingstring.substring(i + 1); permutestring(beginningstring + endingstring.charat(i), newstring); } catch (stringindexoutofboundsexception exception) { exception.printstacktrace(); } }
i getting confused when integer 'i'
gets incremented in loop i.e i=0 1.
1 thing understood first iteration 'i'
got incremented 1 when hit base case i.e
if (endingstring.length() <= 1) system.out.println(beginningstring + endingstring);
i tried debug further , value kept on changing between 0 , 1 successive iterations couldn't understand.
to sum-up, confused regarding relationship between "for loop"
, 2 instructions in try block after first iteration.
i happy if 1 can guide me through process.
i tried elaborate as can drawing hope helpful. recursion doesn't need try-catch block, can remove , work fine
Comments
Post a Comment