Post

[Programmers] #181946 - 문자열 붙여서 출력하기 [Java][C++][Python]

[Programmers] #181946 - 문자열 붙여서 출력하기 [Java][C++][Python]

문제 링크


1. 아이디어

공백으로 구분된 두 문자열 str1, str2를 입력받아 공백 없이 이어붙여 출력하면 되는 문제다.


2. 복잡도

접근시간공간
풀이$O(N)$$O(N)$

($N$ = str1과 str2의 길이 합. C++는 두 문자열을 결합 없이 그대로 출력해 공간 $O(1)$)


3. 코드

풀이 [Java][C++][Python]

1
2
3
4
5
6
7
8
import java.io.*;

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println(br.readLine().replace(" ", ""));
    }
}

BufferedReader로 한줄로 입력을 받은 후 replace로 공백을 제거하는 방식으로 해결했다.

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    string s1, s2;
    cin >> s1 >> s2;
    cout << s1 << s2;
}

C++는 공백 기준으로 값을 읽는 cin >>의 특성을 이용해 두 문자열을 애초에 공백 없이 각각 읽어 들여, 바로 이어붙였다.

1
2
3
4
5
import sys

input = sys.stdin.readline

print(input().replace(" ", ""))

replace로 공백을 제거하는 방식으로 해결했다.


This post is licensed under CC BY 4.0 by the author.