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

VSCode for LeetCode TestCase Template All In One

VSCode for LeetCode TestCase Template All In One

VSCode for LeetCode 测试用例模版

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-07-04
 * @modified
 *
 * @description 459. Repeated Substring Pattern
 * @description 459. 重复的子字符串
 * @difficulty Easy
 * @time_complexity O(n)
 * @space_complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/repeated-substring-pattern/
 * @link https://leetcode-cn.com/problems/repeated-substring-pattern/
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;

/**
 * @param {string} s
 * @return {boolean}
 */
 var repeatedSubstringPattern = function(s) {
  const len = s.length;
  if(len < 2) {
    return false;
  }
  // write your code here
  let temp = '';
  for(let i = 0; i < (len / 2); i++) {
    temp += s[i];
    const reg = new RegExp(temp, 'g');
    const left = s.replace(reg, '');
    if(!left.length) {
      return true;
    }
  }
  return false;
}


// 如果是的话,字符串一定是其子串的整数的倍数, 贪心算法

// 测试用例 test cases
const testCases = [
  {
    input: 'abab',
    result: true,
    desc: 'value equal to true',
  },
  {
    input: 'aba',
    result: false,
    desc: 'value equal to false',
  },
  {
    input: 'abcabcabcabc',
    result: true,
    desc: 'value equal to true',
  },
  {
    input: 'a',
    result: false,
    desc: 'value equal to false',
  },
  {
    input: 'abaababaab',
    // "abaab"
    result: true,
    desc: 'value equal to true',
  },
  {
    input: 'babbabbabbabbab',
    // "bab"
    result: true,
    desc: 'value equal to true',
  },
];

for (const [i, testCase] of testCases.entries()) {
  const result = repeatedSubstringPattern(testCase.input);
  log(`test case i result: `, result === testCase.result ? `✅ passed` : `❌ failed`, result);
  // log(`test case i =`, testCase);
}




KMP 算法


https://en.wikipedia.org/wiki/Knuth–Morris–Pratt_algorithm

https://zh.wikipedia.org/wiki/KMP算法

https://www.geeksforgeeks.org/kmp-algorithm-for-pattern-searching/

refs

https://github.com/xgqfrms/vscode/issues/53



©xgqfrms 2012-2020

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

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


posted @ 2022-07-04 22:43  xgqfrms  阅读(85)  评论(0编辑  收藏  举报