您的位置:首页 > 其它

leetcode 459. Repeated Substring Pattern 暴力拆分即可

2017-12-14 11:14 375 查看
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.)

这道题题意很简单,最长我以为要分析字符的情况,后来发现直接暴力拆分即可,这样更简单,就这么做吧

代码如下:

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <climits>
#include <algorithm>
#include <sstream>
#include <functional>
#include <bitset>
#include <cmath>

using namespace std;

class Solution
{
public:
bool repeatedSubstringPattern(string s)
{
for (int i = s.length() / 2; i >= 1; i--)
{
if (s.length() % i == 0)
{
int c = s.length() / i;
string tmp = "";
for (int j = 0; j < c; j++)
tmp += s.substr(0, i);
if (tmp == s)
return true;
}
}
return false;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: