Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.
It is the substring "abc" four times or the substring "abcabc" twice.
1 <= s.length <= 10000s consists of lowercase English letters.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.
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
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.
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
};
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.
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
}
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.
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;
}
};
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.
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;
}
}
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.
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;
}
}
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.
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
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.
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.
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.