diff options
author | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-03-10 00:36:13 -0600 |
commit | 7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch) | |
tree | eb7a24ee2067f92245c57bb118e85a1baf15df50 /other/repeated_substring_pattern.cpp | |
parent | 5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff) | |
download | leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip |
chore: move cpp and rust code to directory
Diffstat (limited to 'other/repeated_substring_pattern.cpp')
-rw-r--r-- | other/repeated_substring_pattern.cpp | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/other/repeated_substring_pattern.cpp b/other/repeated_substring_pattern.cpp new file mode 100644 index 0000000..4e2d8d8 --- /dev/null +++ b/other/repeated_substring_pattern.cpp @@ -0,0 +1,31 @@ +// 459. Repeated Substring Pattern +#include "leetcode.h" + +/* + * given a string 's', check if it can be constructed by taking a substring of + * it and appending multiple copies of the substring together. + */ + +class Solution { +public: + bool repeatedSubstringPattern(string s) { + int i = 1, j = 0, n = s.size(); + vector<int> dp(n + 1, 0); + while (i < n) { + if (s[i] == s[j]) + dp[++i] = ++j; + else if (!j) + i++; + else + j = dp[j]; + } + return dp[n] && dp[n] % (n - dp[n]) == 0; + } +}; + +int main() { + Solution obj; + printf("%d\n", obj.repeatedSubstringPattern("abab")); // expect: 1 + printf("%d\n", obj.repeatedSubstringPattern("aba")); // expect: 0 + printf("%d\n", obj.repeatedSubstringPattern("abcabcabcabc")); // expect: 1 +} |