forked from noisrucer/noisrucer.github.io_backup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jun Oh Lee
authored and
Jun Oh Lee
committed
May 6, 2022
1 parent
9546a68
commit 428dd27
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
--- | ||
layout: single | ||
title: "[string] Leetcode #20 valid parenthese" | ||
categories: ["string"] | ||
tag: [algorithm, string, leetcode] | ||
toc: true | ||
toc_label: "Table of Contents" | ||
toc_icon: "align-justify" # corresponding Font Awesome icon name (without fa prefix) | ||
toc_sticky: true | ||
--- | ||
|
||
># description of the problem | ||
finding right mate of parenthese | ||
|
||
># logic | ||
using the stack method which means `Last in, first out`. | ||
|
||
```Python | ||
class Solution: | ||
def isValid(self, s: str) -> bool: | ||
cha ={"(":")","[":"]","{":"}"} | ||
stack = [] | ||
for i in range(len(s)): | ||
parenthese = s[i] | ||
if parenthese in cha.keys(): | ||
stack.append(parenthese) | ||
else : | ||
if stack == []: | ||
return False | ||
else: | ||
pop = stack.pop() | ||
if parenthese != cha[pop]: | ||
return False | ||
if stack == []: | ||
return True | ||
|
||
|
||
|
||
``` |