I have written this code to find the possible permutation of string using iterative approach, but it's giving wrong output as follows. Please advise, what's wrong in the code?
Output I'm getting:
- BCAABC ACBABC ABCABC
Expetced output is :
ABC ACB BAC BCA CBA CAB
public class PrintPermutation { public static void main(String[] args) throws Exception { String str = "ABC"; String permutation = str + ""; if (str.length() == 0) { System.out.println("String length is zero and can't make the permutation"); return; } for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); String left = str.substring(0, i); String right = str.substring(i + 1); String Merge = left + right; System.out.println(Merge+ch+permutation); } } }