CodeWars: Permutations Solution
1function swap(arr, i, j) {2 ;[arr[i], arr[j]] = [arr[j], arr[i]]3}45function genPerms(chars, left, right, set) {6 if (left === right) {7 set.add(chars.join(""))8 } else {9 for (let i = left; i <= right; i++) {10 swap(chars, left, i)11 genPerms(chars, left + 1, right, set)12 swap(chars, left, i)13 }14 }15}1617function permutations(string) {18 const N = string.length19 const chars = string.split("")20 const set = new Set()21 genPerms(chars, 0, N - 1, set)22 return Array.from(set)23}
Comments
Loading comments...
Tags
codewars
recursion
backtracking
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Previous Post
LeetCode: Linked List Cycle
Make used of duck typing