recursive permutation

Solutions on MaxInterview for recursive permutation by the best coders in the world

showing results for - "recursive permutation"
Joanne
11 Jul 2020
1function findPerms(str) {  
2      if (str.length === 1) return [str]    
3      let all = []  
4      
5      for (let i = 0; i < str.length; i++) {    
6        const currentLetter = str[i]    
7        const remainingLetters = str.slice(0,i) + str.slice(i+1) 
8        const permsOfRemainingLetters = findPerms(remainingLetters)
9        
10        permsOfRemainingLetters.forEach(
11          subPerm => {all.push(currentLetter + subPerm)}
12        )    
13      }  
14      return all
15    }