[BOJ 2607] ๋น์ทํ ๋จ์ด
1. ๋ฌธ์ : https://www.acmicpc.net/problem/2607
2. ํ์ด
์ํ๋ฒณ์ ๋ฐฐ์ด๋ก ๋ง๋ค๊ณ char์ ์ธ๋ฑ์ค๋ก ๋ฐ๊ฟ์ cnt ๊ณ์ฐ ํ ๋น๊ต
3. ์ฝ๋
package baekjoon;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ๋น์ทํ๋จ์ด {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
String first = br.readLine();
int result = 0;
for(int i = 0; i < N - 1; i++) {
String str = br.readLine();
int cnt = 0; // ๊ฐ์ ๋จ์ด ๊ฐ์
int[] word = new int[26];
for(int j = 0; j < first.length(); j++) {
word[first.charAt(j) - 'A']++;
}
for(int j = 0; j < str.length(); j++) {
if(word[str.charAt(j) - 'A'] > 0) {
cnt++;
word[str.charAt(j) - 'A']--;
}
}
// ๊ธฐ์ค ๋ฌธ์์ด ๊ธธ์ด์ ๋น๊ต ๋ฌธ์์ด ๊ธธ์ด ์ฐจ์ด
// ๊ฐ์ ๊ฒฝ์ฐ or ํ ๊ธ์๋ง ๋ฐ๋๊ฒฝ์ฐ
if(first.length() == str.length() && (first.length() == cnt || first.length() - 1 == cnt)) {
result++;
}
// ๋น๊ต ๋ฌธ์์ด์ด ์งง์ ๊ฒฝ์ฐ (์ ๊ฑฐ)
else if (first.length() == str.length() - 1 && str.length() - 1 == cnt) {
result++;
}
// ๋น๊ต ๋ฌธ์์ด์ด ๊ธด ๊ฒฝ์ฐ (์ถ๊ฐ)
else if (first.length() == str.length() + 1 && str.length() == cnt) {
result++;
}
}
System.out.println(result);
}
}
'๐ป Coding Problems Solving > Array | String | Loop' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[BOJ 2870] ์ํ์์ (0) | 2023.09.13 |
---|---|
[BOJ 24337] ๊ฐํฌ์ ํ (0) | 2023.06.14 |
[BOJ 2141] ์ฐ์ฒด๊ตญ (0) | 2023.06.08 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ์ฐ์ ๋ถ๋ถ ์์ด ํฉ์ ๊ฐ์ (์๋ฐ java) (0) | 2023.02.27 |
[ํ๋ก๊ทธ๋๋จธ์ค] LV.2 ๊ทค ๊ณ ๋ฅด๊ธฐ (์๋ฐ java) (0) | 2023.02.22 |
์ต๊ทผ๋๊ธ