Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add: Repeated Substring Pattern
  • Loading branch information
Openset committed Jan 8, 2019
commit 3ddbad84b955e5f221e15b11b53ddf7fd9349cf9
12 changes: 12 additions & 0 deletions problems/repeated-substring-pattern/repeated_substring_pattern.go
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
package repeated_substring_pattern

import "strings"

func repeatedSubstringPattern(s string) bool {
l := len(s)
for i := 1; i <= l/2; i++ {
if l%i == 0 && strings.Count(s, s[:i]) == l/i {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
@@ -1 +1,54 @@
package repeated_substring_pattern

import "testing"

type caseType struct {
input string
expected bool
}

func TestRepeatedSubstringPattern(t *testing.T) {
tests := [...]caseType{
{
input: "a",
expected: false,
},
{
input: "abab",
expected: true,
},
{
input: "aba",
expected: false,
},
{
input: "abaaba",
expected: true,
},
{
input: "abcabcabcabc",
expected: true,
},
{
input: "abbaabbaabba",
expected: true,
},
{
input: "abcabcabcdabcabcabcdabcabcabcd",
expected: true,
},
{
input: "abaabaabac",
expected: false,
}, {
input: "babbabbabbabbab",
expected: true,
},
}
for _, tc := range tests {
output := repeatedSubstringPattern(tc.input)
if output != tc.expected {
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
}
}
}