728x90
반응형
문제 설명
1. s 문자열이 n개 숫자가 될 때까지 반복된다.
2. 그 문자에서 'a' 의 갯수를 구하면 된다.
3. n의 크기는 크다.
풀이 과정
1. n이 길어서 브루트포스는 안된다.
2. 처음 문자열에서 a의 갯수를 구함
3. n만큼의 길이 내에서 저 문자열이 몇번 반복되는지 구해서 그만큼 곱한다.
4. n에서 반복된 후에 나머지 짜잘한 애들 중 a가 몇개 있는지 구해서 더하면 된다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static long repeatedString(String s, long n) { | |
long aCount = 0; | |
for(int i=0; i<s.length(); i++) { | |
if(s.charAt(i) == 'a') aCount++; | |
} | |
long multiple = n / s.length(); | |
aCount *= multiple; | |
long plus = n % s.length(); | |
for(int i=0; i<plus; i++) { | |
if(s.charAt(i) == 'a') aCount++; | |
} | |
return aCount; | |
} |
반응형
'알고리즘 공부' 카테고리의 다른 글
[백준 1593번] 문자 해독 - java (1) | 2024.10.11 |
---|---|
[HackerRank] 2d-array java 풀이 (1) | 2024.10.09 |
[HackerRank] Jumping on the Clouds java 풀이 (0) | 2024.10.07 |
[백준 5635번] 생일 - java (0) | 2023.02.03 |
[백준 2133번] 타일 채우기 - java (0) | 2022.05.15 |