ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2022-08-14 01:02:54  阅读:217  来源: 互联网

标签:return string 题解 算法 result https com LeetCode goal


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

标签:return,string,题解,算法,result,https,com,LeetCode,goal
来源: https://www.cnblogs.com/xgqfrms/p/16584662.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有