/*****************************************************问题描述*************************************************
Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies
of the substring together. You may assume the given string consists of lowercase English letters only and its length
will not exceed 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.)
判断一个字符串是否可以由其子串的重复拼接得来
/*****************************************************我的解答*************************************************
/**
* @param {string} s
* @return {boolean}
*/
var repeatedSubstringPattern = function(s) {
var repMax = parseInt(s.length);
var repeatStr = function(str,repTimes){
var retStr = str;
for(var index = 1; index < repTimes; index++)
{
retStr += str;
}
return retStr;
}
for(var endIndex = 1; endIndex <= repMax; endIndex++)
{
console.log(endIndex);
var subStr = s.substr(0,endIndex);
console.log('subStr:' + subStr);
for(var rep = 2; rep <= repMax; rep++)
{
var temp = repeatStr(subStr,rep);
console.log('temp:' + temp);
if(s === temp)
{
return true;
}
}
console.log(endIndex);
}
return false;
};
leetCode刷题记录57_459_Repeated Substring Pattern
最新推荐文章于 2019-04-08 15:56:41 发布