LeetCode 旋转字符串算法题解 All In One


LeetCode 旋转字符串算法题解 All In One

js / ts 实现旋转字符串

LeetCode 旋转字符串算法题解 All In One

旋转原理 图解

// 2 倍 s, 一定包含所有(字符移动)旋转操作之后的组合 ✅
// 如, `abc` => `abcabc` (abc, bca, cab)

796. Rotate String

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-08-13
 * @modified
 *
 * @description 796. Rotate String
 * @description 796. 旋转字符串
 * @difficulty Easy
 * @ime_complexity O(n), KMP 算法搜索子字符串的时间复杂度为 O(n)
 * @space_complexity O(n), KMP 算法搜索子字符串的空间复杂度为 O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/rotate-string/
 * @link https://leetcode-cn.com/problems/rotate-string/
 * @solutions
 *
 * @best_solutions
 *
 */

export {};

const log = console.log;

function rotateString(s: string, goal: string): boolean {
  if (s === goal) {
    return true;
  } else if (s.length !== goal.length) {
    return false;
  } else {
    // 2 倍 s, 一定包含所有(字符移动)旋转操作之后的组合 ✅
    // 如, `abc` => `abcabc` (abc, bca, cab)
    // indexOf
    // return (s + s).indexOf(goal) !== -1;
    // return s.repeat(2).indexOf(goal);
    // return s.padEnd(s.length * 2, s).indexOf(goal);
    // includes
    // return (s + s).includes(goal) !== -1;
    // return s.repeat(2).includes(goal);
    return s.padEnd(s.length * 2, s).includes(goal);
  }
};

/*
function rotateString(s: string, goal: string): boolean {
  if(s === goal) {
    return true;
  }
  const arr = [];
  const strs = goal.split('');
  // index order,防止存在重复 char,导致 index 计算错误 ✅
  for(let [index, str] of strs.entries()) {
    // right + left
    arr.push(goal.slice(index) + goal.slice(0, index));
    // 优化: 提前终止,if mactch, 较少循环次数
    if(arr.includes(s)) {
      return true;
    }
  }
  return arr.includes(s);
};

*/


// type alias
type ObjectType = {
  inputs: [string, string];
  result: boolean;
  desc: string;
}
interface TestCaseInterface extends Array<ObjectType> {
  //
}
// interface TestCaseInterface extends Array<any> {
//   [index: number]: ObjectType;
// }

// 测试用例 test cases
const testCases: TestCaseInterface = [
  {
    inputs: ["abcde", "cdeab"],
    result: true,
    desc: 'value equal to true',
  },
  {
    inputs: ["abcde", "abced"],
    result: false,
    desc: 'value equal to false',
  },
];
for (const [i, testCase] of testCases.entries()) {
  const [first, second] = testCase.inputs;
  const result = rotateString(first, second);
  log(`test case i result: /n`, result === testCase.result ? `passed ✅` : `failed ❌`, result);
  // log(`test case i =`, testCase);
}


https://leetcode.com/problems/rotate-string/
https://leetcode.cn/problems/rotate-string/

leetcode 题解 / LeetCode Solutions

https://www.youtube.com/results?search_query=+Leetcode+796

<iframe allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture” allowfullscreen=”” frameborder=”0″ height=”315″ src=”https://www.youtube.com/embed/M04IJUf_jEQ?start=5″ title=”YouTube video player” width=”560″></iframe>

https://www.youtube.com/playlist?list=PLamwFu9yMruCBtS2tHUD77oI_Wsce-syE

https://www.youtube.com/channel/UCftIXZeipv4MTVwmfFphtYw/videos

https://neetcode.io/

类似问题

LeetCode 28. Implement strStr()

https://leetcode.com/problems/implement-strstr/

<iframe allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture” allowfullscreen=”” frameborder=”0″ height=”315″ src=”https://www.youtube.com/embed/5zHw-ALj3-k” title=”YouTube video player” width=”560″></iframe>

refs



Flag Counter


©xgqfrms 2012-2020

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

原创文章,版权所有©️xgqfrms, 禁止转载

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/280401.html

(0)
上一篇 2022年8月14日 02:21
下一篇 2022年8月14日 02:21

相关推荐

发表回复

登录后才能评论