zoukankan      html  css  js  c++  java
  • 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern【easy】

    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.)

    一开始理解为只有abcabc这种情况了,搞了一个错误代码

     1 class Solution {
     2 public:
     3     bool repeatedSubstringPattern(string s) {
     4         int len = s.size();
     5         if (len % 2) {
     6             return false;
     7         }
     8         
     9         return s.substr(0, len / 2) == s.substr(len / 2);
    10     }
    11 };

    但题意是说: constructed by taking a substring of it and appending multiple copies of the substring together,如果按上面错误解法,abcabc这种字符串是可以判断正确的,但是对于abcdabcdabcd这种字符串就无能为力了。

    解法一:

     1 class Solution {
     2 public:
     3     bool repeatedSubstringPattern(string str) {
     4         string nextStr = str;
     5         int len = str.length();
     6         if(len < 1) return false;
     7         for(int i = 1; i <= len / 2; i++){
     8             if(len % i == 0){
     9                 nextStr = leftShift(str, i);
    10                 if(nextStr == str) return true;
    11             }
    12         }
    13         return false;
    14     }
    15     
    16     string leftShift(string &str, int l){
    17         string ret = str.substr(l);
    18         ret += str.substr(0, l);
    19         return ret;
    20     }
    21 };

    参考了@shell32的解法。 

    对于每个小长度进行判断,可以被整个长度整除,说明该小长度有可能成为备选;下面就是如何通过这个备选来看是否可以由多个备选组成整个字符串了。这个解法我们就是用到了左移再合并字符串的方法。

    解法二:

    1 bool repeatedSubstringPattern(string str) {
    2     int n = str.length();
    3     for (int i = 1; i <= n / 2; i++)
    4         if (n % i == 0 && str.substr(i) == str.substr(0, n - i))
    5             return true;
    6     return false;
    7 }

    参考了@StefanPochmann的解法。

    解法一中判断:“如何通过这个备选来看是否可以由多个备选组成整个字符串了”的方法,解法二直接采用字符串区间的方法来判断。

    解法三:

    1 bool repeatedSubstringPattern(string str) 
    2     {
    3         return (str + str).substr(1, str.size() * 2 - 2).find(str)!=-1;
    4     }

    这是一个大神解法,参考了@ Xianming.Chen的解法。

    他的思路如下:

    str + str means doubling, (str + str).substr(1, str.size() * 2 - 2) means removing the first char of the first half and the last char of the second half.

    1. If there is no pattern, both of the first copy and the second copy will be changed, so str will not be found in (str + str).substr(1, str.size() * 2 - 2).
    2. If there is a pattern, the first char of str can still be found in the first half, and the last char of str can also be found in the second half. Here is an example: abcabc is the original string, and (bcabc abcab) includes abcabc.

    对于上面的情况1,例子如下:

    abaabc

    baabcabaab

    可以发现,根本找不到

    对于上面的情况2,例子如下:

    abcabc

    bcabcabcab

    可以发现,能找到

    另外补充一下,C++ string截取字符串的函数:

    s.substr(pos, n):截取s中从pos开始(包括0)的n个字符的子串,并返回

    s.substr(pos):截取s中从从pos开始(包括0)到末尾的所有字符的子串,并返回

  • 相关阅读:
    [CentOS5]安装VirtualBox
    [Java]一则自定义的XStream转换器,主要用于POJO XML反序列化为Map/List
    [CentOS5]开启vsftpd中本地用户的上传权限
    [CSS]强制TD不换行
    [CentOS5]快速关闭SeLinux
    [vba]获取PPT幻灯片中的所有标题
    [VBA]批量转换xls为csv
    [OTHER]玩具的报复 绿化版 注册表
    [CentOS]在vsftpd中为本地用户指定默认目录
    递归读取输出无限分类目录
  • 原文地址:https://www.cnblogs.com/abc-begin/p/7580878.html
Copyright © 2011-2022 走看看