leetcode 字符串的全排列 All In One


leetcode 字符串的全排列 All In One

LeetCode 567. 字符串的排列

// 排列组合

字符串的排列

给你两个字符串 s1 和 s2 ,写一个函数来判断 s2 是否包含 s1 的排列。如果是,返回 true ;否则,返回 false 。

https://leetcode.cn/explore/interview/card/bytedance/242/string/1016/

https://leetcode.cn/problems/permutation-in-string/

https://leetcode.cn/problems/zi-fu-chuan-de-pai-lie-lcof/

https://wizardforcel.gitbooks.io/the-art-of-programming-by-july/content/01.06.html

数学乐

排列组合

https://www.shuxuele.com/combinatorics/combinations-permutations.html

Combinations and Permutations

组合和排列

https://www.mathsisfun.com/combinatorics/combinations-permutations.html

// 阶乘函数

一、重复排列

二、不重复排列

不可以重复,选择可能每次减少一个。

???

/**
 * @param {string} s
 * @return {string[]}
 */
var permutation = function(s) {
  const set = new Set();
  const arr = [...s];
  const length = s.length;
  // a, b, c
  // 排列组合,阶乘!
  function dfs(c, s, left = true) {
    // 递归 c, s => c + s / s + c;
    if(s.length > 1) {
      return left ? c + dfs(s[0], s.substring(1)) : dfs(s[0], s.substring(1)) + c;
    }
    return left ? c + s : s + c;
  }
  for(let c of arr) {
    const temp = arr.filter(i => i !== c);
    // temp 排序
    for(let i = 0; i < temp.length; i++) {
      // 滑动窗口
      // let l = c + temp.join('');
      // let r = temp.join('') + c;
      let l = dfs(c, temp.join(''), true);
      let r = dfs(c, temp.join(''), false);
      if(!set.has(l)) {
        set.add(l);
      }
      if(!set.has(r)) {
        set.add(r);
      }
    }
  }
  return [...set];
};

// var permutation = function(s) {
//   const set = new Set();
//   const arr = [...s];
//   set.add(s);
//   set.add(arr.reverse().join(''));
//   for(let c of arr) {
//     let temp = [];
//     for(let i = 0; i < arr.length; i++) {
//       // string 是原始值,不可以直接修改值 ?
//       if(arr[i] !== c) {
//         temp.push(arr[i]);
//       }
//     }
//     for(let j = 0; j < temp.length; j++) {
//       temp[j] = temp[j] + c;
//     }
//     if(!set.has(temp.join(''))) {
//       set.add(temp.join(''));
//     }
//   }
//   return [...set];
// };

// var permutation = function(s) {
//   const set = new Set();
//   for(let c of s) {
//     for(let i = 0; i < s.length; i++) {
//       // string 是原始值,不可以直接修改值 ?
//       const temp = [...s];
//       if(emp[i] !== c) {
//         temp[i] = c;
//       }
//       // subString
//       if(!set.has(temp.join(''))) {
//         set.add(temp.join(''));
//       }
//     }
//   }
//   return [...set];
// };

refs


Flag Counter

?xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有??xgqfrms, 禁止转载 ???,侵权必究??!