-
Notifications
You must be signed in to change notification settings - Fork 3
/
0459-repeated-substring-pattern.rb
47 lines (38 loc) · 1.06 KB
/
0459-repeated-substring-pattern.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# frozen_string_literal: true
# 459. Repeated Substring Pattern
# Easy
# https://leetcode.com/problems/repeated-substring-pattern
=begin
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 = "abab"
Output: true
Explanation: It is the substring "ab" twice.
Example 2:
Input: s = "aba"
Output: false
Example 3:
Input: s = "abcabcabcabc"
Output: true
Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
Constraints:
1 <= s.length <= 104
s consists of lowercase English letters.
=end
# @param {String} s
# @return {Boolean}
def repeated_substring_pattern(s)
str = s + s
str[1...-1].include? s
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_repeated_substring_pattern < Test::Unit::TestCase
def test_
assert_equal true, repeated_substring_pattern("abab")
assert_equal false, repeated_substring_pattern("aba")
assert_equal true, repeated_substring_pattern("abcabcabcabc")
end
end