๋ฌธ์
https://www.acmicpc.net/problem/1316
ํ์ด
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class ๊ทธ๋ฃน๋จ์ด์ฒด์ปค {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException{
int count = 0;
int N = Integer.parseInt(br.readLine());
for (int i = 0; i < N; i++) {
if (check() == true) {
count++;
}
}
System.out.println(count);
}
public static boolean check() throws IOException {
boolean[] check = new boolean[26];
int prev = 0;
String str = br.readLine();
for(int i = 0; i < str.length(); i++) {
int now = str.charAt(i); // i ๋ฒ์งธ ๋ฌธ์ ์ ์ฅ (ํ์ฌ ๋ฌธ์)
// ์์ ๋ฌธ์์ i ๋ฒ์งธ ๋ฌธ์๊ฐ ๊ฐ์ง ์๋ค๋ฉด?
if (prev != now) {
// ํด๋น ๋ฌธ์๊ฐ ์ฒ์ ๋์ค๋ ๊ฒฝ์ฐ (false ์ธ ๊ฒฝ์ฐ)
if ( check[now - 'a'] == false ) {
check[now - 'a'] = true; // true ๋ก ๋ฐ๊ฟ์ค๋ค
prev = now; // ๋ค์ ํด์ ์ํด prev ๋ ๋ฐ๊ฟ์ค๋ค
}
// ํด๋น ๋ฌธ์๊ฐ ์ด๋ฏธ ๋์จ ์ ์ด ์๋ ๊ฒฝ์ฐ (๊ทธ๋ฃน๋จ์ด๊ฐ ์๋๊ฒ ๋จ)
else {
return false; //ํจ์ ์ข
๋ฃ
}
}
// ์์ ๋ฌธ์์ i ๋ฒ์งธ ๋ฌธ์๊ฐ ๊ฐ๋ค๋ฉด? (์ฐ์๋ ๋ฌธ์)
// else ๋ฌธ์ ์์ด๋ ๋จ
else {
continue;
}
}
return true;
}
}
'๐๏ธ Algorithm > ๐ฉ ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๐ฉ [๋ฐฑ์ค] [Java] [Silver2] 1260๋ฒ_DFS์ BFS (0) | 2024.01.08 |
---|---|
๐ฉ [๋ฐฑ์ค] [Java] [Silver5] 2941๋ฒ_ํฌ๋ก์ํฐ์ ์ํ๋ฒณ (0) | 2023.12.10 |
๐ฉ [๋ฐฑ์ค] [Java] [Silver5] 4673๋ฒ_์ ํ ๋๋ฒ (0) | 2023.12.07 |
๐ฉ [๋ฐฑ์ค] [Python] [Silver1] 13335๋ฒ_ํธ๋ญ (0) | 2023.06.29 |
๐ฉ [๋ฐฑ์ค] [Python] [Gold4] 15685๋ฒ_๋๋๊ณค ์ปค๋ธ (0) | 2023.06.26 |