东拼西凑网站谁做的,微信网站小游戏,怎么做和京东一样网站,公司网站app怎么做Every day a Leetcode
题目来源#xff1a;2981. 找出出现至少三次的最长特殊子字符串 I
解法1#xff1a;滑动窗口 暴力枚举
滑动窗口枚举窗口内字符相同的字符串#xff0c;再暴力枚举长度相等的字符串。
代码#xff1a;
/** lc appleetcode.cn id2981 langcpp**…Every day a Leetcode
题目来源2981. 找出出现至少三次的最长特殊子字符串 I
解法1滑动窗口 暴力枚举
滑动窗口枚举窗口内字符相同的字符串再暴力枚举长度相等的字符串。
代码
/** lc appleetcode.cn id2981 langcpp** [2981] 找出出现至少三次的最长特殊子字符串 I*/// lc codestartclass Solution
{
public:int maximumLength(string s){// 特判if (s.empty())return 0;int n s.size();int ans -1, left 0;for (int right 0; right n; right){while (s[left] ! s[right])left;int len right - left 1;string sub s.substr(left, len);int count 1;// 暴力枚举所有的子字符串for (int i left 1; i n; i){string temp s.substr(i, len);if (sub temp){count;if (count 3){ans max(ans, len);break;}}}}return ans;}
};
// lc codeend结果 复杂度分析
时间复杂度O(n2)其中 n 是字符串 s 的长度。
空间复杂度O(1)。