Arena / Problems /

459. Repeated Substring Pattern

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #459

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

Example 1
Input: s = "abcabcabcabc"
Output: true
Explanation:

It is the substring "abc" four times or the substring "abcabc" twice.

Example 2
Input: s = "aba"
Output: false
Constraints
  • 1 <= s.length <= 10000
  • s consists of lowercase English letters.
Topics & Algorithms
⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Python Solution by Anonymous Developer
0
Approach Intuition:

The key to solving this problem is to see clearly that if s can be obtained by repeating the substring, then the starting letter of the substring must be s[0].

Once you understand this, the scope of substring investigation is greatly narrowed.

class Solution:
    def repeatedSubstringPattern(self, s: str) -> bool:
        for i in range(1, int(len(s) / 2) + 1):
            if len(s) % i == 0 and s[:i] * int(len(s) / i) == s:
                return True

        return False
⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Javascript Solution by Anonymous Developer
0
Approach Intuition:

The key to solving this problem is to see clearly that if s can be obtained by repeating the substring, then the starting letter of the substring must be s[0].

Once you understand this, the scope of substring investigation is greatly narrowed.

var repeatedSubstringPattern = function (s) {
  for (let i = 1; i <= s.length / 2; i++) {
    if (s.length % i != 0) {
      continue
    }

    if (s.slice(0, i).repeat(s.length / i) == s) {
      return true
    }
  }

  return false
};
⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Go Solution by Anonymous Developer
0
Approach Intuition:

The key to solving this problem is to see clearly that if s can be obtained by repeating the substring, then the starting letter of the substring must be s[0].

Once you understand this, the scope of substring investigation is greatly narrowed.

func repeatedSubstringPattern(s string) bool {
    n := len(s)

    for i := 1; i <= n/2; i++ {
        if n%i == 0 {
            substring := s[:i]
            repeated := strings.Repeat(substring, n/i)

            if repeated == s {
                return true
            }
        }
    }

    return false
}
⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C++ Solution by Anonymous Developer
0
Approach Intuition:

The key to solving this problem is to see clearly that if s can be obtained by repeating the substring, then the starting letter of the substring must be s[0].

Once you understand this, the scope of substring investigation is greatly narrowed.

class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        int n = s.length();

        for (int i = 1; i <= n / 2; i++) {
            if (n % i != 0) {
                continue;
            }

            string pattern = s.substr(0, i);
            string repeated = "";

            for (int j = 0; j < n / i; j++) {
                repeated += pattern;
            }

            if (repeated == s) {
                return true;
            }
        }

        return false;
    }
};
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Java Solution by Anonymous Developer
0
Approach Intuition:

The key to solving this problem is to see clearly that if s can be obtained by repeating the substring, then the starting letter of the substring must be s[0].

Once you understand this, the scope of substring investigation is greatly narrowed.

class Solution {
    public boolean repeatedSubstringPattern(String s) {
        int n = s.length();

        for (var i = 1; i <= n / 2; i++) {
            if (n % i != 0) {
                continue;
            }

            var pattern = s.substring(0, i);
            var repeated = new StringBuilder();

            // Simply concatenate the pattern multiple times
            for (var j = 0; j < n / i; j++) {
                repeated.append(pattern);
            }

            // Compare the constructed string with the original string
            if (repeated.toString().equals(s)) {
                return true;
            }
        }

        return false;
    }
}
⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative C# Solution by Anonymous Developer
0
Approach Intuition:

The key to solving this problem is to see clearly that if s can be obtained by repeating the substring, then the starting letter of the substring must be s[0].

Once you understand this, the scope of substring investigation is greatly narrowed.

public class Solution
{
    public bool RepeatedSubstringPattern(string s)
    {
        int n = s.Length;

        for (int i = 1; i <= n / 2; i++)
        {
            if (n % i != 0)
            {
                continue;
            }

            // Get the potential substring pattern
            string pattern = s.Substring(0, i);
            StringBuilder repeated = new StringBuilder();

            // Simply concatenate the pattern multiple times
            for (int j = 0; j < n / i; j++)
            {
                repeated.Append(pattern);
            }

            // Compare the constructed string with the original string
            if (repeated.ToString() == s)
            {
                return true;
            }
        }

        return false;
    }
}
⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Alternative Ruby Solution by Anonymous Developer
0
Approach Intuition:

The key to solving this problem is to see clearly that if s can be obtained by repeating the substring, then the starting letter of the substring must be s[0].

Once you understand this, the scope of substring investigation is greatly narrowed.

# @param {String} s
# @return {Boolean}
def repeated_substring_pattern(s)
  (0...s.size / 2).each do |i|
    next unless s.size % (i + 1) == 0

    if s[0..i] * (s.size / (i + 1)) == s
      return true
    end
  end

  false
end
⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #459 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.