Re: [閒聊] 每日LeetCode

作者: Rushia (みけねこ的鼻屎)   2023-08-21 18:02:48
https://leetcode.com/problems/repeated-substring-pattern/description/
459. Repeated Substring Pattern
給你一個字串s,判斷s是否可以被切成多個完全相同的子字串。
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.
思路:
1.如果一個s可以被切成多個子字串,那麼他的:
1) 長度一定大於1
2) 子字串長度一定小於等於s長度/2
3) 子字串長度一定可以整除s長度
2.利用上面三點去切出所有長度為 1~s長度除二 的子串,並判斷全由子字串組成的新字串
是否等於s即可。
Java Code:

Links booklink

Contact Us: admin [ a t ] ucptt.com