# Generating permutations [duplicate]

Active3 hr before
Viewed126 times

generatingpermutations
90%

coding-interview , coding-interview , coding-interview ,Permutation with repeats in result is actually Cartesian Product,

Example_snippet/controller/utility/_generating.js/ ab = aa, bb, ab, ba abc = aaa,. . .
```ab = aa, bb, ab, ba
abc = aaa, aab, abc...```
88%

You are still generating all the permutations(including the duplicates), The question mentions that the solution should avoid doing that

Example_snippet/controller/utility/_generating.js/ circularPermutations::[a]->[[a. . .
```circularPermutations::[a] - > [
[a]
]
circularPermutations xs = helper[] xs[]
where helper acc[] _ = acc
helper acc(x: xs) ys =
helper(((x: xs) ++ys): acc) xs(ys++[x])

nrPermutations::[(Int, a)] - > [
[a]
]
nrPermutations xs = concat(map helper(circularPermutations xs))
where helper((1, x): xs) = map((: ) x)(nrPermutations xs)
helper((n, x): xs) = map((: ) x)(nrPermutations((n - 1, x): xs))```
72%

Print all distinct permutations of a string having duplicates,,Print all distinct permutations of a given string with duplicates,Distinct permutations of the string | Set 2,Write a program to print all permutations of a given string

Example_snippet/controller/utility/_generating.js/ Input : ABCA Output : AABC AAC. . .
```Input: ABCA
Output: AABC AACB ABAC ABCA ACBA
ACAB BAAC BACA BCAA CABA
CAAB CBAA```
Step 2 continued with i = 0 1 2 3     A B A C index . . .
```i = 0 1 2 3
A B A C
index = 0, s[0] = A
Start swapping s[index] with s[i] following it:
i = index + 1 = 1

Since s[index] != s[i], swap and recur.

i = 2, s[index] == s[i], don 't swap

i = 3, s[index] != s[i], swap and recur.```
Step 3 continued with ABCA ABAC ACBA ACAB AACB AABC . . .
```ABCA
ABAC
ACBA
ACAB
AACB
AABC
BACA
BAAC
BCAA
CBAA
CABA
CAAB```
Step 4 continued with aabc aacb abac abca acba acab . . .
```aabc
aacb
abac
abca
acba
acab
baac
baca
bcaa
caba
caab
cbaa
Total distinct permutations = 12```
65%

If you generate permutations in lexicographical order, then you don't need to store anything to work out whether the reverse of a given permutation has already been seen, You just have to lexicographically compare it to its reverse - if it's smaller then return it, if it's larger then skip it

Example_snippet/controller/utility/_permutations.js/ for p in itertools.permutation. . .
```for p in itertools.permutations(range(3)):
if p[0] <= p[-1]:
print(p)```
75%

We are required to write a JavaScript function that takes in a string str, Our function should create all permutations of the input string and remove duplicates, if present

Example_snippet/controller/utility/_permutations.js/ const str = 'aabb'; const perm. . .
```const str = 'aabb';
const permute = (str = '') => {
if (!!str.length && str.length < 2) {
return str
}
const arr = [];
for (let i = 0; i < str.length; i++) {
let char = str[i]
if (str.indexOf(char) != i)
continue
let remainder = str.slice(0, i) + str.slice(i + 1, str.length)
for (let permutation of permute(remainder)) {
arr.push(char + permutation)
}
}
return arr
}
console.log(permute(str));```