
1. 오늘의 학습 키워드
[LeetCode] 921. Minimum Add to Make Parentheses Valid/스택/큐/JAVA 풀이
2. 오늘의 학습 문제
문제
https://leetcode.com/problems/minimum-add-to-make-parentheses-valid/description/
921. Minimum Add to Make Parentheses Valid
A parentheses string is valid if and only if:
- It is the empty string,
- It can be written as AB (A concatenated with B), where A and B are valid strings, or
- It can be written as (A), where A is a valid string.
You are given a parentheses string s. In one move, you can insert a parenthesis at any position of the string.
- For example, if s = "()))", you can insert an opening parenthesis to be "(()))" or a closing parenthesis to be "())))".
Return the minimum number of moves required to make s valid.
Example 1:
Input: s = "())" Output: 1
Example 2:
Input: s = "(((" Output: 3
Constraints:
- 1 <= s.length <= 1000
- s[i] is either '(' or ')'.
코드
/* 1. 스택을 만든다. 2. 스택에 s의 원소 하나씩 삽입 3. )이 주어졌을 때 stack.peek()가 (이면 pop 4. 스택 크기를 return */ import java.util.*; class Solution { public int minAddToMakeValid(String s) { Stack <Character> stack =new Stack<>(); //스택 선언 for(char c : s.toCharArray()){ if(c == ')' && !stack.isEmpty() && stack.peek() == '('){ stack.pop(); } else stack.push(c); } return stack.size(); } }
3. 오늘의 회고
- 이전에 풀었던 괄호 문제와 유사하다.
- 스택을 통해 풀었다.
https://pudingcoding.tistory.com/74
99클럽 코테 스터디 4일차 TIL + 스택/프로그래머스 [올바른 괄호]
1. 오늘의 학습 키워드 스택 2. 오늘의 학습 문제 문제https://school.programmers.co.kr/learn/courses/30/lessons/12909/ 프로그래머스코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개
pudingcoding.tistory.com
프로그래머스 유사한 문제 풀이