20.4.13 Simple repetition of substrings


Time complexity will not be analyzed, above O (n²), space complexity O (n)

topic

Given a non-empty string, determine whether it can be composed of one of its substrings repeated multiple times. The given string contains only lowercase English letters, and the length does not exceed 10000.

Example 1:
Input: "abab"
Output: True
Explanation: It can be composed of the substring "ab" repeated twice.

Example 2:
Input: "aba"
Output: False

Example 3:
Input: "abcabcabcabc"
Output: True
Explanation: It can be formed by repeating the substring "abc" four times. (Or the substring "abcabc" is repeated twice.)

Problem-solving ideas

  1. Use a string to store substrings. The substring must be at the beginning of the string, so every time the substring is removed from the string and the beginning substring is followed by a string with the same length of substring for comparison. If the string is the same, the string jumps by another substring length and continues Compare. Until the end of the comparison or different;
  2. If they are not the same, update the substring, add the first character after the substring, and continue 1 operation;
  3. The time complexity is too high, so the length of the string is added to find the length of the remaining substring to determine whether it is a substring.

Code ideas

  1. Exclude special circumstances;
  2. The first character of the substring subStr is stored as a substring;
  3. For loop, find the remainder first. If the length of the substring is not 0, then the current subStr is definitely not a substring. Add one more character and continue the loop;
  4. If the remainder is 0, it may be a substring. Perform substring comparison, and after the substring that has been compared in the original string, take a string of the same length as the substring for comparison. If it is a substring, the traversal pointer jumps to the end of the successfully compared substring; if it is not a substring, the traversal pointer returns to the end of the original substring of the original string and updates the substring subStr;
  5. After the end of the loop, if the length of subStr is equal to the length of the original string, an error is returned, and if it is not equal, the return is correct;
  6. According to my own thinking, this is probably the way to write it, but the time complexity is too high, and writing code like this also looks messy. Another method is to traverse the substrings according to the possible number of substrings after finding the remainder of the substrings. This way of writing is much clearer.

Code

class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        if(s.length()==1) return false;

        string subStr;
        subStr.push_back(s[0]);

        for(int i=1; i<s.length(); i++){
            if(s.length()%subStr.length()){
                subStr.push_back(s[i]);
                continue;
            } 
            if(subStr==s.substr(i,subStr.length())){
                i+=subStr.length()-1;
                continue;
            } 
            else{
                subStr.append(s.substr(subStr.length(),1));
                i=subStr.length()-1;
            } 
        }
        if(subStr.length()==s.length()) return false;
        
        return true;
    }
};

Guess you like

Origin www.cnblogs.com/wasi-991017/p/12692943.html