01-24 00:19
Recent Posts
Recent Comments
๊ด€๋ฆฌ ๋ฉ”๋‰ด

miinsun

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_35 ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ ๋ณธ๋ฌธ

Algorithm/Java

[Algorithm]์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ž๋ฐ”_35 ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ

miinsun 2022. 1. 5. 19:52

 

๐Ÿ’ฌ ๋ฌธ์ œ ์„ค๋ช…

๊ด„ํ˜ธ๊ฐ€ ์ž…๋ ฅ๋˜๋ฉด ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ์ด๋ฉด “YES", ์˜ฌ๋ฐ”๋ฅด์ง€ ์•Š์œผ๋ฉด ”NO"๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
(())() ์ด๊ฒƒ์€ ๊ด„ํ˜ธ์˜ ์Œ์ด ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์œ„์น˜ํ•˜๋Š” ๊ฑฐ์ง€๋งŒ, (()()))์€ ์˜ฌ๋ฐ”๋ฅธ ๊ด„ํ˜ธ๊ฐ€ ์•„๋‹ˆ๋‹ค.

 

 

๐Ÿ”จ ์ž…์ถœ๋ ฅ ์˜ˆ

์ž…๋ ฅ - ์ฒซ ๋ฒˆ์งธ ์ค„์— ๊ด„ํ˜ธ ๋ฌธ์ž์—ด์ด ์ž…๋ ฅ๋ฉ๋‹ˆ๋‹ค. ๋ฌธ์ž์—ด์˜ ์ตœ๋Œ€ ๊ธธ์ด๋Š” 30์ด๋‹ค.

(()(()))(()

์ถœ๋ ฅ - ์ฒซ ๋ฒˆ์งธ ์ค„์— YES, NO๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

NO

 

 

โ€‹

๐Ÿ’ป Solution.java

import java.util.*;

public class Main {

	public String solution(String s) {
		String answer = "NO";
	
		Stack<String> st = new Stack<>();
		for(char c : s.toCharArray()) {
			if(c == ')') {
				if(st.contains("("))
					st.pop();
				else
					st.push(")");
			}
			else {
				st.push("(");
			}
		}
		
		if(st.isEmpty())
			answer = "YES";
		
        return answer;
	}
	
	public static void main(String[] args){
		Main main  = new Main();
		Scanner sc =new Scanner(System.in);
		
		String s = sc.next();
		
		System.out.println(main.solution(s));
		
		sc.close();
		return ;
	}
}

 

 

 

Comments