# Generating permutations [duplicate]

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

ab = aa, bb, ab, ba abc = aaa, aab, abc...

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

```
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 x | length x == 1 = [take(fst(head x))(repeat(snd(head x)))]
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))
```

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

```
Input: ABCA
Output: AABC AACB ABAC ABCA ACBA
ACAB BAAC BACA BCAA CABA
CAAB CBAA
```

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.

ABCA ABAC ACBA ACAB AACB AABC BACA BAAC BCAA CBAA CABA CAAB

aabc aacb abac abca acba acab baac baca bcaa caba caab cbaa Total distinct permutations = 12

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

```
for p in itertools.permutations(range(3)):
if p[0] <= p[-1]:
print(p)
```

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

```
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));
```

#### Other "generating-permutations" answers related to "Generating permutations [duplicate]"

- Which type hint expresses that an attribute must not be None?
- Capturing Python process's exit status in UNIX shell
- Flask - How to display a selected dropdown value in same html page?
- Proper way of handling JSON Parsing TypeError when element does not exist
- ValueError: Could not find the input entity for PeerUser
- Draw rotated font with pygame
- Overwrite points from pcolormesh if they aren't contained in a polygon
- Rotating (or different) User-Agent is giving the Error: AttributeError: 'NoneType' object has no attribute 'text'
- How to retrieve more than one row with the checked checkbox in QTableView
- Run Python3 without activating the virtual environment
- Opens same registry twice?
- Why does my Python turtle shape size decrease when pressing 'shift'
- Python/Selenium/PhantomJS - Data retained between execution
- Scikit-learn - multinomial logistic regression with probabilities as a target variable
- Tensorflow apply different activation functions in output layer
- How to obtain user input in Python?
- Creating HDF5 compound attributes using h5py
- Tensorflow-gpu not using GPU while fitting model
- Plotting multiple lines of streaming data in a bokeh server application
- Google Assistant Installation on Python3.6 OSX