Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
Example 1:
Input: "abab" Output: True Explanation: It's the substring "ab" twice.
Example 2:
Input: "aba" Output: False
Example 3:
Input: "abcabcabcabc" Output: True Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)<Solution>
目前看到這篇的解法最簡明
概念就是,如果字串 s 真的是由 substr 重復組合成的
那麼 s 的第一個字就是 substr 的第一個字
s 的最後一個字就是 substr 的最後一個字
令 s1 = s + s,並且把 s1 的第一個字和最後一個字拿掉後
則 s 一定會是 s1 的 substr
code 如下
C++
Java
Kotlin
沒有留言:
張貼留言