Post

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

공백으로 구분된 두 문자열을 이어붙여 출력하는 워밍업 문제.

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

문제 링크


1. 아이디어

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


2. 복잡도

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

($N$ = str1, str2 길이의 합)


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(" ", ""));
    }
}
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(" ", ""))

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