Backtracking Programming - Check with all feasibles. Dynamic Programming we will choose optimal chosen.
These days in coding exams very popular. Even though you are talented, it will help sharp your skills and ask companies more salary.
If we take the (ABC) --> How many all possible ways to combination, This problem belongs to backtracking.
Backtracking with DFS approach I had taken. DFS (Stack) - means first we will one branch we will complete that branch means here (ABC,0,0) - We will complete all possibilites then we will choose (BAC,0,1) like this way.
For this type of try to understand conceptual logic, it's very tricky here combination of for loop with recursion. for loop --> sequence, recursion --> stack (DFS)
Forumula:
Deriving the recursive with for loop forumula, I am attaching image.
p(ABC,0,2) --> for loop ( (ABC,0,0) (BAC,0,1) (CAB,0,2) )
p(ABC,1,2) --> for ( (swapped str, 1,1) (swapped str,1,2)) like this
Mixing of recursion with for loop
package optumtest.permutation;
public class PermutationTest {
public static void main(String[] args) {
String str = "ABC";
int n = str.length();
PermutationTest permutation = new PermutationTest();
permutation.permute(str, 0, n-1);
}
int ram = 0;
private void permute(String str, int start, int end) {
System.out.println("("+str+","+start+","+end+")");
if (start == end) {
System.out.println(str);
} else {
for (int i = start; i <= end; i++) {
str = swap(str, start, i);
permute(str, start + 1, end);
str = swap(str, start, i);
}
}
}
public String swap(String a, int start, int end) {
char temp;
char[] charArray = a.toCharArray();
temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
return String.valueOf(charArray);
}
}
These days in coding exams very popular. Even though you are talented, it will help sharp your skills and ask companies more salary.
If we take the (ABC) --> How many all possible ways to combination, This problem belongs to backtracking.
Backtracking with DFS approach I had taken. DFS (Stack) - means first we will one branch we will complete that branch means here (ABC,0,0) - We will complete all possibilites then we will choose (BAC,0,1) like this way.
For this type of try to understand conceptual logic, it's very tricky here combination of for loop with recursion. for loop --> sequence, recursion --> stack (DFS)
Forumula:
Deriving the recursive with for loop forumula, I am attaching image.
p(ABC,0,2) --> for loop ( (ABC,0,0) (BAC,0,1) (CAB,0,2) )
p(ABC,1,2) --> for ( (swapped str, 1,1) (swapped str,1,2)) like this
Mixing of recursion with for loop
package optumtest.permutation;
public class PermutationTest {
public static void main(String[] args) {
String str = "ABC";
int n = str.length();
PermutationTest permutation = new PermutationTest();
permutation.permute(str, 0, n-1);
}
int ram = 0;
private void permute(String str, int start, int end) {
System.out.println("("+str+","+start+","+end+")");
if (start == end) {
System.out.println(str);
} else {
for (int i = start; i <= end; i++) {
str = swap(str, start, i);
permute(str, start + 1, end);
str = swap(str, start, i);
}
}
}
public String swap(String a, int start, int end) {
char temp;
char[] charArray = a.toCharArray();
temp = charArray[start];
charArray[start] = charArray[end];
charArray[end] = temp;
return String.valueOf(charArray);
}
}
No comments:
Post a Comment