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

2022/8/14 1:53:19

本文主要是介绍LeetCode 旋转字符串算法题解 All In One,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

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

js / ts 实现旋转字符串

旋转原理 图解

// 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, 禁止转载



这篇关于LeetCode 旋转字符串算法题解 All In One的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程