코린이의 소소한 공부노트

[LeetCode/Easy] 1668. Maximum Repeating Substring 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 1668. Maximum Repeating Substring

무지맘 2023. 6. 19. 10:47

1. Input

1) String sequence

2) String word

 

2. Output

1) wordsequence에서 반복되는 최대 수를 반환

 

3. Constraint

1) 1 <= sequence.length <= 100

2) 1 <= word.length <= 100

3) sequenceword는 영어 소문자로 이루어져 있다.

 

4. Example

Input: sequence = "ababc", word = "ab" -> Output: 2

Input: sequence = "ababc", word = "ba" -> Output: 1

Input: sequence = "ababc", word = "ac" -> Output: 0

 

5. Code

1) 첫 코드(2023/06/19)

class Solution {
    public int maxRepeating(String sequence, String word) {
        int ans = 0, rep = sequence.length()/word.length();
        while(rep>0){
            if(sequence.contains((word.repeat(rep)))){
                ans = rep; break;
            }
            rep--;
        }
        return ans;
    }
}

- 91%, 60%