Title Description
Description Title
input a character string to print all the characters in the string are arranged in lexicographic order. For example input string abc, abc print all strings of characters a, b, c can be arranged out, acb, bac, bca, cab and cba.
Input Description:
enter a string of not more than 9 (possibly repeated characters), characters include only lowercase letters.
Thinking
Based on the idea of backtracking
js code
function Permutation(str)
{
// write code here
if (str === '') return []
let arr = str.split('')
let res = []
Help(arr, 0)
return res.sort()
function Help(arr, i) {
if (i === arr.length-1) {
if (!res.includes(arr.join(''))) {
res.push(arr.join(''))
return
}
}
for(let j = i; j < arr.length; j++) {
swap(arr, i, j)
Help(arr, i+1)
swap(arr, i, j)
}
}
function swap (arr, i, j) {
let temp = arr[i]
arr[i] = arr[j]
arr[j] = temp
}
}