Algorithm

LeetCode 459. Repeated Substring Pattern

쿠케캬캬 2023. 8. 21. 21:07
반응형

https://leetcode.com/problems/repeated-substring-pattern/description/

 

Repeated Substring Pattern - LeetCode

Can you solve this real interview question? Repeated Substring Pattern - 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 Expl

leetcode.com

 

패턴의 길이가 i일 때, 반복되는 동일한 패턴으로 s를 만들 수 있는지 확인해주었습니다.

class Solution {
public:
    bool repeatedSubstringPattern(string s) {
        int sz = s.size();
        for(int i=1; i<sz; i++) {
            if(sz % i != 0) continue;
            bool flag = true;
            for(int j=i; j<sz; j++) {
                if(s[j % i] != s[j]) {
                    flag = false;
                    break;
                }
            }
            if(flag) return true;
        }

        return false;
    }
};
반응형