Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VSCode for LeetCode TestCase Template All In One #53

Open
xgqfrms opened this issue Jul 4, 2022 · 0 comments
Open

VSCode for LeetCode TestCase Template All In One #53

xgqfrms opened this issue Jul 4, 2022 · 0 comments
Labels
vscode & leetcode vscode & leetcode vscode vscode 测试用例模版 测试用例模版

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Jul 4, 2022

VSCode for LeetCode TestCase Template All In One

VSCode for LeetCode 测试用例模版

image

"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);
}


@xgqfrms xgqfrms added vscode vscode vscode & leetcode vscode & leetcode 测试用例模版 测试用例模版 labels Jul 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
vscode & leetcode vscode & leetcode vscode vscode 测试用例模版 测试用例模版
Projects
None yet
Development

No branches or pull requests

1 participant