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

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/

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 發佈文章使用:只允許註冊用戶纔可以訪問!

原創文章,版權所有©️xgqfrms, 禁止轉載 🈲️,侵權必究⚠️!


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章