import java.util.Stack;

public class Solution {
    public static int solution(int[][] board, int[] moves){
        Stack<Integer> stack = new Stack<>();
        int answer = 0;

        for(int i=0; i<moves.length; i++){
            int idx = moves[i]-1;
            for(int a=0; a<board.length; a++){
                if(board[a][idx]!=0){
                    int temp = board[a][idx];
                    board[a][idx] = 0;
                    if(!stack.isEmpty() && temp==stack.peek()){
                        answer+=2;
                        stack.pop();
                    } else {
                        stack.add(temp);
                    }
                    break;
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        int[][] board = {
                {0,0,0,0,0},
                {0,0,1,0,3},
                {0,2,5,0,1},
                {4,2,4,4,2},
                {3,5,1,3,1}
        };

        int[] moves = {1,5,3,5,1,2,1,4};
        System.out.println(T.solution(board,moves));
    }
}
import java.util.Stack;

// 괄호가 입력되면 올바른 괄호는 YES 아니면 NO 출력
public class Solution {
    public static String solution(String str){
        Stack<Character> stack = new Stack<>();
        String answer = "YES";

        for(int i=0; i<str.length(); i++){
            char element = str.charAt(i);
            if(stack.isEmpty()){
                stack.add(element);
            } else {
                if(!stack.peek().equals(element)){
                    stack.pop();
                } else {
                    stack.add(element);
                }
            }
        }
        if(!stack.isEmpty()){
            answer = "NO";
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        System.out.println(T.solution("(()(()))(()"));
    }
}

 

 

import java.util.Stack;

public class Solution {
    public static String solution(String str){
        Stack<Character> stack = new Stack<>();
        String answer = "";

        for(int i=0; i<str.length(); i++){
            char element = str.charAt(i);

            if(stack.isEmpty()){
               stack.add(element);
            } else {
                if(element==')') {
                    while (stack.pop() != '(');
                } else {
                    stack.add(element);
                }
            }
        }

        for(int i=0; i<stack.size(); i++){
            answer += stack.get(i);
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        System.out.println(T.solution("(A(BC)D)EF(G(H)(IJ)K)LM(N)"));
    }
}

String(정수) to Int

- int num = Integer.parseInt(str);

- int num = Integer.valueOf(str);

 

Int to String

- String str = Integer.toString(i);

- String str = String.valueOf(i);

 

Char to Int

int num = (int)(ch-'0'); // 0은 48

 

Int to Char

- char ch = (char)(num+'0');

 

String to char

- String str = "A";

- char[] ch = str.toCharArray();

- ch[0]; // 'A'

 

Double to String

- String str = Double.toString(db);

 

String to Double

- double db = Double.valueOf(str).doubleValue();

 

long to String

- String str = Long.toString(l);

 

String to long 

- long l = Long.valueOf(str).longValue();

- long l = Long.parseLong(str);

 

float to String

- String str = Float.toString(f);

 

String to float

- float f = Float.valueOf(str).floatValue();

 

ASCII Code to String

- String str = new Character((char)num).toString();

 

String to ASCII Code

- int num = (int)ch;

 

10진수 -> 2진수

- String binaryStr = Integer.toBinaryString(num);

 

public String solution(String s){
    String answer = "";

    for(int i=0; i<4; i++) {
        String temp = s.substring(0, 7).replace('#', '1').replace('*', '0');
        // 문자를 잘라주고, 1. 이진수로 바꿔준다.

        int num = Integer.parseInt(temp, 2);
        // 2진수->10진수

        answer += (char) num;
        // 아스키코드 변환 int형을 char으로 강제변환

        s = s.substring(7);
        // 앞에 문자 자르기

    }

    return answer;
}
public int[] solution(String s, char t){
    int[] answer = new int[s.length()];
    int p = Integer.MAX_VALUE;

    for(int i=0; i<s.length(); i++){
        char ch = s.charAt(i);
        if(ch==t){ // 동일하면 거리 0
            p=0;
            answer[i] = p;
        } else { // 동일하지 않을 경우
            p++; // 거리 증가
            answer[i] = p;
        }
    }

    return answer;
}
public String solution(String str){
    String answer = "YES";
    str = str.toLowerCase();

    for(int i=0; i<str.length()/2; i++){
        char leftElement = str.charAt(i);
        char rightElement = str.charAt(str.length()-i-1);
        if(leftElement!=rightElement){
            answer = "NO";
            break;
        }
    }

    return answer;
}

 

정규 표현식

- 알파벳 제외하고 모두 제거하기

[^A-Z]

// 정규 표현식 : 특수 문자 제거 [^A-Z]
str = str.toUpperCase().replaceAll("[^A-Z]", "");

기존 풀이 방법

public class Solution {
    static ArrayList<Character> list = new ArrayList<>();
    public String solution(String str){
        String answer = "";
        StringBuilder sb = new StringBuilder();

        for(int i=0; i<str.length(); i++){
            char element = str.charAt(i);

            if(!list.contains(element)){
                list.add(element);
                sb.append(element);
            }
        }
        answer = sb.toString();
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        System.out.println(T.solution("ksekkset"));
    }
}

 

새로운 풀이 방법

- indexof를 사용해서 제일 특정 문자의 첫 번째 인덱스를 받아와서 출력해준다.

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

 

public class Solution {
    public String solution(String str){
        String answer = "";
        char[] arr = str.toCharArray();
        int lp =0, rp = str.length()-1;

        while(lp<rp){
            if(!Character.isAlphabetic(arr[lp])){
                lp++;
            } else if(!Character.isAlphabetic(arr[rp])){
                rp--;
            } else { // 둘 다 알파벳
                char temp = arr[lp];
                arr[lp] = arr[rp];
                arr[rp] = temp;
                lp++;
                rp--;
            }
        }
        answer = String.valueOf(arr);
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        System.out.println(T.solution("a#b!GE*T@s"));
    }
}

+ Recent posts