Tcdian / keep

今天不想做,所以才去做。
MIT License
5 stars 1 forks source link

459. Repeated Substring Pattern #311

Open Tcdian opened 3 years ago

Tcdian commented 3 years ago

459. Repeated Substring Pattern

给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成。给定的字符串只含有小写英文字母,并且长度不超过10000。

Example 1

Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.

Example 2

Input: "aba"
Output: False

Example 3

Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
Tcdian commented 3 years ago

Solution

/**
 * @param {string} s
 * @return {boolean}
 */
var repeatedSubstringPattern = function(s) {
    return /^([a-z]+?)\1+$/.test(s);
};
function repeatedSubstringPattern(s: string): boolean {
    return /^([a-z]+?)\1+$/.test(s);
};