/*
* Rishi Verma
* 19-Feb-2015
*
* http://w...content-available-to-author-only...s.org/write-a-c-program-to-print-all-permutations-of-a-given-string/
*/
class PrintStringCombi {
static char s[] = { 'a', 'b', 'c' };
/* Function to swap values at two pointers */
static void swap(int i, int j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
}
/*
* Function to print permutations of string This function takes three
* parameters: 1. String 2. Starting index of the string 3. Ending index of
* the string.
*/
static void permute(int i) {
int j;
if (i == s.length - 1)
else {
for (j = i; j < s.length; j++) {
swap(i, j);
permute(i + 1);
swap(i, j); // backtrack
}
}
}
/* Driver program to test above functions */
public static void main
(String[] args
) { permute(0);
}
}
LyoKICogUmlzaGkgVmVybWEKICogMTktRmViLTIwMTUKICogCiAqIGh0dHA6Ly93Li4uY29udGVudC1hdmFpbGFibGUtdG8tYXV0aG9yLW9ubHkuLi5zLm9yZy93cml0ZS1hLWMtcHJvZ3JhbS10by1wcmludC1hbGwtcGVybXV0YXRpb25zLW9mLWEtZ2l2ZW4tc3RyaW5nLwogKi8KIGNsYXNzIFByaW50U3RyaW5nQ29tYmkgewoKCXN0YXRpYyBjaGFyIHNbXSA9IHsgJ2EnLCAnYicsICdjJyB9OwoKCS8qIEZ1bmN0aW9uIHRvIHN3YXAgdmFsdWVzIGF0IHR3byBwb2ludGVycyAqLwoJc3RhdGljIHZvaWQgc3dhcChpbnQgaSwgaW50IGopIHsKCQljaGFyIHRlbXAgPSBzW2ldOwoJCXNbaV0gPSBzW2pdOwoJCXNbal0gPSB0ZW1wOwoJfQoKCS8qCgkgKiBGdW5jdGlvbiB0byBwcmludCBwZXJtdXRhdGlvbnMgb2Ygc3RyaW5nIFRoaXMgZnVuY3Rpb24gdGFrZXMgdGhyZWUKCSAqIHBhcmFtZXRlcnM6IDEuIFN0cmluZyAyLiBTdGFydGluZyBpbmRleCBvZiB0aGUgc3RyaW5nIDMuIEVuZGluZyBpbmRleCBvZgoJICogdGhlIHN0cmluZy4KCSAqLwoJc3RhdGljIHZvaWQg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