Welcome to Subscribe On Youtube

Formatted question description: https://leetcode.ca/all/459.html

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

Related Topics:
String

Similar Questions:

Solution 1.

  • class Solution {
        public boolean repeatedSubstringPattern(String s) {
            if (s == null || s.length() <= 1)
                return false;
            int length = s.length();
            StringBuffer subStringBuffer = new StringBuffer();
            int maxSublength = length / 2;
            for (int i = 0; i < maxSublength; i++) {
                subStringBuffer.append(s.charAt(i));
                int curLength = i + 1;
                if (length % curLength != 0)
                    continue;
                int times = length / curLength;
                StringBuffer temp = new StringBuffer();
                for (int j = 0; j < times; j++)
                    temp.append(subStringBuffer);
                if (temp.toString().equals(s))
                    return true;
            }
            return false;
        }
    }
    
    ############
    
    class Solution {
        public boolean repeatedSubstringPattern(String s) {
            String str = s + s;
            return str.substring(1, str.length() - 1).contains(s);
        }
    }
    
    
  • // OJ: https://leetcode.com/problems/repeated-substring-pattern/
    // Time: O(N^2)
    // Space: O(1)
    class Solution {
    public:
        bool repeatedSubstringPattern(string s) {
            int N = s.size();
            for (int len = 1; len <= N / 2; ++len) {
                if (N % len) continue;
                int i = len;
                for (; i < N; ++i) {
                    if (s[i] != s[i % len]) break;
                }
                if (i == N) return true;
            }
            return false;
        }
    };
    
  • class Solution:
        def repeatedSubstringPattern(self, s: str) -> bool:
            return (s + s).index(s, 1) < len(s)
    
    ############
    
    class Solution(object):
    
      def repeatedSubstringPattern(self, str):
        """
        :type str: str
        :rtype: bool
        """
        for i in range(0, len(str) / 2):
          if not len(str) % (i + 1) and str[:i + 1] * (len(str) / (i + 1)) == str:
            return True
        return False
    
    
  • func repeatedSubstringPattern(s string) bool {
    	return strings.Index(s[1:]+s, s) < len(s)-1
    }
    
  • function repeatedSubstringPattern(s: string): boolean {
        return (s + s).slice(1, (s.length << 1) - 1).includes(s);
    }
    
    
  • impl Solution {
        pub fn repeated_substring_pattern(s: String) -> bool {
            (s.clone() + &s)[1..s.len() * 2 - 1].contains(&s)
        }
    }
    
    

All Problems

All Solutions