Published:
Updated:

평소 코딩 테스트는 파이썬으로 연습했지만, 자바로도 할 수 있도록 대비하기 위함입니다.


문자 찾기 Solution

import java.util.*Scanner*;

public class Main {

    public int solution(String str, char c) {
        int answer = 0;

        str = str.toLowerCase();
        c = Character.toLowerCase(c);

        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) == c) {
                answer++;
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Main T = new Main();
        Scanner kb = new Scanner(System.in);

        String str = kb.next();
        char c = kb.next().charAt(0);

        System.out.print(T.solution(str, c));
    }

}


대소문자 변환 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        StringBuilder answer = new StringBuilder();

        for (char c : input.toCharArray()) {
            if (Character.isLowerCase(c)) {
                answer.append(Character.toUpperCase(c));
                continue;
            }

            answer.append(Character.toLowerCase(c));
        }

        return answer.toString();
    }

    public static void main(String[] args) {
        Main T = new Main();
        Scanner in = new Scanner(System.in);
        String input = in.next();
        System.out.print(T.solution(input));
    }

}


문장 속 단어 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        String[] words = input.split(" ");
        Arrays.sort(words, (a, b) -> b.length() - a.length());
        return words[0];
    }

    public static void main(String[] args) {
        Main T = new Main();
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(T.solution(input));
    }

}


단어 뒤집기 Solution

import java.util.*;

public class Main {

    public String[] solution(String[] inputs, int N) {
        String[] answer = new String[N];

        for (int i = 0; i < N; i++) {
            char[] currentArray = inputs[i].toCharArray();

            char tmp;
            int lt = 0;
            int rt = currentArray.length - 1;

            while (lt < rt) {
                tmp = currentArray[lt];
                currentArray[lt] = currentArray[rt];
                currentArray[rt] = tmp;

                lt += 1;
                rt -= 1;
            }

            answer[i] = String.valueOf(currentArray);
        }

        return answer;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int N = Integer.parseInt(in.nextLine());
        String[] inputs = new String[N];
        for (int i = 0; i < N; i++) {
            inputs[i] = in.nextLine();
        }

        Main T = new Main();
        for (String x : T.solution(inputs, N)) {
            System.out.println(x);
        }
    }

}


특정 문자 뒤집기 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        char[] inputCharArray = input.toCharArray();
        char tmp;
        int lt = 0;
        int rt = inputCharArray.length - 1;

        while (lt < rt) {
            // 좌포인터에서 특수문자라면 넘어가고
            if (!Character.isAlphabetic(inputCharArray[lt])) {
                lt += 1;
                continue;
            }

            // 우포인터에서도 특수문자라면 넘어가고
            if (!Character.isAlphabetic(inputCharArray[rt])) {
                rt -= 1;
                continue;
            }

            // 특수문자가 아닌, 알파벳이라면 일반적인 투포인터 뒤집기 사용
            tmp = inputCharArray[lt];
            inputCharArray[lt] = inputCharArray[rt];
            inputCharArray[rt] = tmp;
            lt += 1;
            rt -= 1;
        }

        return String.valueOf(inputCharArray);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(new Main().solution(input));
    }
}


중복 문자 제거 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        StringBuilder answer = new StringBuilder();

        char[] inputCharArray = input.toCharArray();
        Set<Character> hashSet = new HashSet<>();

        for (char c : inputCharArray) {
            if (!hashSet.contains(c)) {
                answer.append(c);
                hashSet.add(c);
            }
        }

        return answer.toString();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(new Main().solution(input));
    }

}


회문 문자열 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        final String YES = "YES";
        final String NO = "NO";

        input = input.toLowerCase();

        if (sameReverse(input)) {
            return YES;
        }

        return NO;
    }

    public boolean sameReverse(String input) {
        char[] inputCharArray = input.toCharArray();
        char tmp;
        int lt = 0;
        int rt = inputCharArray.length - 1;

        while (lt < rt) {
            tmp = inputCharArray[lt];
            inputCharArray[lt] = inputCharArray[rt];
            inputCharArray[rt] = tmp;

            lt += 1;
            rt -= 1;
        }

        return input.equals(String.valueOf(inputCharArray));
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(new Main().solution(input));
    }

}


유효한 팰린드롬 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        input = input.toLowerCase();

        final String YES = "YES";
        final String NO = "NO";

        char[] inputCharArray = input.toCharArray();
        char tmp;
        int lt = 0;
        int rt = inputCharArray.length - 1;

        while (lt < rt) {
            if (!Character.isAlphabetic(inputCharArray[lt])) {
                lt += 1;
                continue;
            }

            if (!Character.isAlphabetic(inputCharArray[rt])) {
                rt -= 1;
                continue;
            }

            tmp = inputCharArray[lt];
            inputCharArray[lt] = inputCharArray[rt];
            inputCharArray[rt] = tmp;

            lt += 1;
            rt -= 1;
        }

        if (String.valueOf(inputCharArray).equals(input)) {
            return YES;
        }

        return NO;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(new Main().solution(input));
    }

}


숫자만 추출 Solution

import java.util.*;

public class Main {

    public int solution(String input) {
        StringBuilder answer = new StringBuilder();

        char[] inputCharArray = input.toCharArray();
        for (char c : inputCharArray) {
            if (Character.isDigit(c)) {
                answer.append(c);
            }
        }

        return Integer.parseInt(answer.toString());
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(new Main().solution(input));
    }

}


가장 짧은 문자 거리 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        StringBuilder answer = new StringBuilder();

        String[] split = input.split(" ");
        String s = split[0];
        char t = split[1].charAt(0);

        char[] sCharArray = s.toCharArray();
        // location: 문자 t가 있는 위치를 0부터 표시한 집합
        Set<Integer> location = new HashSet<>();
        for (int i = 0; i < sCharArray.length; i++) {
            if (sCharArray[i] == t) {
                location.add(i);
            }
        }

        for (int i = 0; i < sCharArray.length; i++) {
            int minDistance = Integer.MAX_VALUE;
            for (Integer each : location) {
                // 거리의 최솟값이 결국 떨어진 거리라는 의미
                minDistance = Math.min(minDistance, Math.abs(each - i));
            }

            answer.append(minDistance).append(" ");
        }

        return answer.toString();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(new Main().solution(input));
    }

}


문자열 압축 Solution

import java.util.*;

public class Main {

    public String solution(String input) {
        StringBuilder answer = new StringBuilder();

        char[] charArray = input.toCharArray();
        char current = '0';
        int cnt = 1;

        int index = 0;
        for (char c : charArray) {
            if (c == current) {
                cnt += 1;

                if (index == charArray.length - 1) {
                    answer.append(current).append(cnt);
                }
            } else {
                if (cnt == 1) {
                    if (current != '0') {
                        answer.append(current);
                    }
                } else {
                    answer.append(current).append(cnt);
                    cnt = 1;
                }

                current = c;
            }

            index += 1;
        }

        return answer.toString();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        System.out.println(new Main().solution(input));
    }

}


암호 Solution

import java.util.*;

public class Main {

    public String solution(int n, String input) {
        StringBuilder answer = new StringBuilder();
        StringBuilder binaryStr = new StringBuilder();
        char[] inputCharArray = input.toCharArray();
        int currentDecimal = 0;

        for (char c : inputCharArray) {
            if (c == '#') {
                binaryStr.append('1');
            } else {
                binaryStr.append('0');
            }

            if (binaryStr.length() == 7) {
                currentDecimal = Integer.parseInt(binaryStr.toString(), 2);
                binaryStr = new StringBuilder();
                answer.append((char) currentDecimal);
            }

        }

        return answer.toString();
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = Integer.parseInt(in.nextLine());
        String input = in.nextLine();
        System.out.println(new Main().solution(n, input));
    }
}


Reference

Leave a comment