[Programmers] 181949번 - 대소문자 바꿔서 출력하기 [Java][C++]
[Programmers] 181949번 - 대소문자 바꿔서 출력하기 [Java][C++]
1. 아이디어
각 문자가 소문자면 대문자로, 대문자면 소문자로 아스키 오프셋만큼 이동시켜 변환해주면 된다.
2. 복잡도
| 시간복잡도 | 공간복잡도 |
|---|---|
| $O(N)$ | $O(1)$ |
$N$ =
str길이
3. 코드
풀이 [Java][C++]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.io.*;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
for (char c : br.readLine().toCharArray()) {
if ('a' <= c && c <= 'z') {
sb.append((char) (c - 'a' + 'A'));
} else {
sb.append((char) (c - 'A' + 'a'));
}
}
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
#include <iostream>
#include <string>
using namespace std;
int main(void) {
ios::sync_with_stdio(0);
cin.tie(0);
string s, ans;
cin >> s;
for (char c : s) {
if ('a' <= c && c <= 'z') {
ans += (char)(c - 'a' + 'A');
} else {
ans += (char)(c - 'A' + 'a');
}
}
cout << ans;
}
This post is licensed under CC BY 4.0 by the author.