s
, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.Example 1:
Example 2:
1 <= s.length <= 10^4
s
consists of lowercase English letters./**
* @param {string} s
* @return {boolean}
*/
var repeatedSubstringPattern = function(s) {
const totalLenth = s.length;
for (let i = 1; i <= Math.floor(totalLenth/2); i++) {
const str = s.substring(0, i);
const repeatedStr = str.repeat(totalLenth/i)
if (repeatedStr == s) {
return true;
}
}
return false;
};
nums
and an integer k
, return the maximum number of consecutive 1
's in the array if you can flip at most k
0
's.Example 1:
Example 2:
1 <= nums.length <= 10^5
nums[i]
is either 0
or 1
.0 <= k <= nums.length
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var longestOnes = function(nums, k) {
let left = 0;
let right = 0;
while (right < nums.length) {
if (nums[right] === 0) {
k--;
}
if (k < 0) {
if (nums[left] === 0) {
k++;
}
left++;
}
right++;
}
return right - left;
};
s
, find the length of the longest substring without repeating characters.Example 1:
Example 2:
0 <= s.length <= 5 * 10^4
s
consists of English letters, digits, symbols and spaces./**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
const strSet = new Set();
let maxLength = 0;
let left = 0;
for (let right = 0; right < s.length; right++) {
const str = s[right];
while (strSet.has(str)) {
strSet.delete(s[left]);
left++;
}
strSet.add(str);
maxLength = Math.max(maxLength, right - left + 1);
}
return maxLength;
};