[코테 풀이] Repeated Substring Pattern

시내·2024년 6월 7일
0

Q_459) Repeated Substring Pattern

출처 : https://leetcode.com/problems/repeated-substring-pattern/?envType=study-plan-v2&envId=programming-skills

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 boolean repeatedSubstringPattern(String s) {
        String substring = "";
        for (int i = 1; i < s.length(); i++) {
            substring = s.substring(0, i);
            int repeated = s.length() / substring.length();
            String altogether = "";
            for (int r = 0; r < repeated; r++) {
                altogether += substring;
            }
            if (altogether.equals(s)) {
                return true;
            }
        }
        return false;
    }
}
profile
contact 📨 ksw08215@gmail.com

0개의 댓글