Created
March 18, 2026 18:09
-
-
Save adi928/236eb64cc8a87571642666e3eac52298 to your computer and use it in GitHub Desktop.
For review mutable hawk
This file contains hidden or 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
| // Solving valid paranthese with char array and topPtr - 10 min | |
| public boolean isValid(String s) { | |
| char[] charArr = new char[s.length()]; | |
| int topPtr = -1; | |
| // to add : charArr[+topPtr++] = c | |
| for (char c : s.toCharArray()) { | |
| switch(c) { | |
| case ')' -> { | |
| if (topPtr < 0 || charArr[topPtr] != '(') return false; | |
| topPtr--; | |
| } | |
| case ']' -> { | |
| if (topPtr < 0 || charArr[topPtr] != '[') return false; | |
| topPtr--; | |
| } | |
| case '}' -> { | |
| if (topPtr < 0 || charArr[topPtr] != '{') return false; | |
| topPtr--; | |
| } | |
| default -> { | |
| charArr[++topPtr] = c; | |
| } | |
| } | |
| } | |
| return topPtr == -1; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment