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.)
这道题题意很简单,最长我以为要分析字符的情况,后来发现直接暴力拆分即可,这样更简单,就这么做吧
代码如下:
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; } };
相关文章推荐
- leetcode 459. Repeated Substring Pattern 重复子串的确定 + 不需要寻找重复出现的子串 + 暴力拼接即可
- leetcode 149. Max Points on a Line 计算斜率的问题 + 直接暴力求解即可
- leetcode 463. Island Perimeter 周长统计 + 暴力统计即可
- leetcode 699. Falling Squares 俄罗斯方块的最高高度 + 暴力遍历即可
- [leetcode] 290. Word Pattern
- hfut1190真心最水同余方程,直接暴力即可
- leetcode面试准备: Word Pattern
- Leetcode 290. Word Pattern (Easy) (cpp)
- LeetCode 290 word pattern 的一点疑惑
- leetcode343-Integer Break(整数拆分求最大乘积)
- leetcode_290. Word Pattern 单词模式识别,识别句子中的单词模式,单词与模式的一一映射
- HDU 4509 湫湫系列故事——减肥记II(暴力模拟即可)
- 【LeetCode】290.Word Pattern解题报告
- leetcode 719. Find K-th Smallest Pair Distance 第k小的绝对距离 + 暴力计算真棒
- 【12月22日】LeetCode刷题日志(六):132 Pattern
- Leetcode 第290题Word Pattern
- 【leetcode】459. Repeated Substring Pattern【E】
- [LeetCode]48. Word Pattern匹配模式
- [LeetCode] Word Break II 拆分词句之二
- leetcode 290. Word Pattern 使用HashMap编码