[BaekJoon] #21867 - Java Bitecode [Java][C++]
[BaekJoon] #21867 - Java Bitecode [Java][C++]
1. 아이디어
주어진 문자열에서 J, A, V를 뺀 나머지 문자들로 새로운 문자열을 만드는 방식으로 해결할 수 있다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(N)$ | $O(N)$ |
($N$ = s의 길이)
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
br.readLine();
String s = br.readLine();
for (char c : s.toCharArray()) {
if (c == 'J' || c == 'A' || c == 'V') continue;
sb.append(c);
}
if (sb.length() == 0) {
System.out.println("nojava");
} else {
System.out.println(sb);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
string s;
cin >> n >> s;
string res;
for (char c : s) {
if (c == 'J' || c == 'A' || c == 'V') continue;
res += c;
}
if (res.empty()) {
cout << "nojava";
} else {
cout << res;
}
}
This post is licensed under CC BY 4.0 by the author.