xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

leetcode 字符串的全排列 All In One

leetcode 字符串的全排列 All In One

LeetCode 567. 字符串的排列

// 排列组合

// s1 和 s2 仅包含小写字母
function checkInclusion(s1: string, s2: string): boolean {
    const len1 = s1.length;
    const len2 = s2.length;
    // 如果 s1 长度大于 s2, 直接返回 false
    if (len1 > len2) {
      return false;
    }
    // 构造 a-z 26 个小写字母的集合
    const count = Array(26).fill(0);
    // const count = [...``.padEnd(26, 0)].map(i => 0);
    // const count = [...``.padEnd(26, 0)].map(Number);
    // 统计 a-z 26 个小写字母中,每个字母分别出现的次数
    for (let i = 0; i < len1; i++) {
      // 一个加
      count[s1.charCodeAt(i) - 97]++;
      //一个减
      count[s2.charCodeAt(i) - 97]--;
    }
    // 如果一加一减全部消除掉,s1 中每个出现的字母都为 0, 直接返回 true
    if (count.every(i => i === 0)) {
      return true;
    }
    // if (!count.some(i => i !== 0)) {
    //   return true;
    // }
    // s2 > s1, 继续遍历 s2 剩余的字母字符
    for (let i = len1; i < len2; i++) {
      // 先减一
      count[s2.charCodeAt(i) - 97]--;
      // 后加一, ??? 从 0 开始加 ❓❓❓
      count[s2.charCodeAt(i - len1) - 97]++;
      // 如果一加一减全部消除掉,s1 中每个出现的字母都为 0, 直接返回 true
      if (count.every(i => i === 0)) {
        return true;
      }
      // if (!count.some(e => e !== 0)) {
      //   return true;
      // }
    }
    return false;
};

/*


function checkInclusion(s1: string, s2: string): boolean {
  // 排列组合 
  function permutation(s: string): string[] {
    const len = s.length;
    if(len === 1) {
      return [s];
    }
    const result: string[] = [];
    for(let i = 0; i < len; i++) {
      let current = s[i];
      let others = s.slice(0, i) + s.slice(i + 1);
      const temp = permutation(others);
      for(let j = 0; j < temp.length; j++) {
        const str = current + temp[j];
        // 去重
        if(!result.includes(str)) {
          result.push(str);
        }
      }
    }
    return result;
  };
  const strs = permutation(s1);
  for(let i = 0; i < strs.length; i++) {
    if(s2.includes(strs[i])) {
      return true;
    }
  }
  return false;
};

*/

字符串的排列

给你两个字符串 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://leetcode.cn/problems/permutation-in-string/solution/zi-fu-chuan-de-pai-lie-by-leetcode-solut-7k7u/

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

阶乘

// 阶乘函数

function factorial(n) {
  if(n ===  0) return  1;
  if(n === 1) return 1;
  return n * factorial(n-1);
}

// n! = n × (n−1)!


// 阶乘函数(符号:!)的意思是把逐一减小的自然数序列相乘
function factorial(n) {
  if(n === 1) {
    return 1;
  }
  return n * factorial(n - 1);
};

// 尾调用优化 / 尾递归优化
function factorial(n, multi = 1) {
  if(n === 1) {
    return multi;
  }
  multi = n * multi;
  return factorial(n - 1, multi);
};
      

https://www.cnblogs.com/xgqfrms/p/16756767.html

https://www.mathsisfun.com/numbers/factorial.html

https://www.shuxuele.com/numbers/factorial.html

https://www.cnblogs.com/xgqfrms/tag/阶乘/

排列组合

Combinations and Permutations / 组合和排列

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

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

一、重复排列

二、不重复排列

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

js 字符串全排列


function getFullPermutation(str) {
  if (str.length === 1) {
    return [str];
  } else {
    const result = [];
    //遍历每一项
    for (let i = 0; i < str.length; i++) {
      // 当前的元素
      let current = str[i];
      // 其他元素 (拼接)
      let others = str.slice(0, i) + str.slice(i + 1);
      // 其他元素的全排列 (递归)
      let temp = getFullPermutation(others);
      for (let j = 0; j < temp.length; j++) {
        // 组合
        const item = current + temp[j];
        result.push(item);
      }
    }
    return result;
  } 
}

// test
const str = `abc`;
getFullPermutation(str);

https://www.cnblogs.com/xgqfrms/p/16557404.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

https://www.cnblogs.com/xgqfrms/p/16557404.html

https://www.cnblogs.com/xgqfrms/p/16372741.html

数学乐

本网站旨在覆盖整个幼儿园高中数学课程

https://www.shuxuele.com/about.html

https://www.shuxuele.com/links/index-curriculum.html



©xgqfrms 2012-2020

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

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-06-19 22:57  xgqfrms  阅读(125)  评论(2编辑  收藏  举报