r/learnprogramming • u/shaynjam • Oct 16 '26
Solved im kinda proud of this solution i wrote even though it was an "easy" i still felt like i grew a lot
the problem:
Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
- Every close bracket has a corresponding open bracket of the same type.
Example 1:
Input: s = "()"
Output: true
Example 2:
Input: s = "()[]{}"
Output: true
Example 3:
Input: s = "(]"
Output: false
Example 4:
Input: s = "([])"
Output: true
Example 5:
Input: s = "([)]"
Output: false
my solution: (on leetcode)
class Solution:
def isValid(self, s: str) -> bool:
inputs = {
"(":1,
")":9,
"{":2,
"}":8,
"[":3,
"]":7}
prev_bracket=[]
if(len(s)%2!=0):
return False
for a in s:
if inputs[a]<4:
prev_bracket.append(inputs[a])
else:
if len(prev_bracket)==0:
return False
x=prev_bracket.pop()
if x+inputs[a]!=10:
return False
return len(prev_bracket)==0
208
Upvotes
36
u/shaynjam Oct 16 '26
im sorry i marked it as nsfw since i couldnt find how to make it as a spoiler
85
u/ALargeSpork Oct 16 '26
You should be proud of yourself! That feeling is addictive and the reason so many of us got into programming to begin with!
Good job btw in figuring this out. Keep going and never lose the passion!
35
u/balefrost Oct 16 '26
You should be proud, this is a good solution!
As somebody who reviews a lot of code, I'd like to point out two things:
I think it's worth keeping in mind the difference between "intent" and "mechanism". In the first case, you are trying to answer the question "is this an open bracket". That's the intent. And the mechanism you use is to check that it maps to a value less than 4. Similarly, in the second case, the question you are answering is "is this the correct closing bracket". And the mechanism you use is to construct
inputso that matching pairs add to 10.When somebody is reading your code, they will be working backwards. They start from the mechanism and infer the intent. They see
if x+inputs[a]!=10:and need to mentally turn that intoif this is the correct closing brace for the most recent opening brace:. And at least on the surface, that10seems like a magic constant. Why 10? What does that have to do with brackets? It only makes sense if you also have the content ofinputsin mind.I think a key part of making code easy to understand is to minimize the conceptual "distance" between intent and mechanism. You can do that by adding comments. Or you can extract well-named variables and functions (e.g.
if isMatchingBracket(x, a):). Or maybe you can structure your code in a way that minimizes the conceptual distance.Somebody else already mentioned this: in this problem, for every open bracket that has not yet been closed, there's exactly one bracket that can close it. Currently, in your stack (
prev_bracket), you store a number that can be used to determine if some other bracket is the correct closing bracket. But another option is to just store the expected closing brackets on the stack. Don't store1 2 3. Store) } ].To be clear, your code here is small enough that it's not hard to read at all. And "sums to 10" is a clever solution. As you work on bigger and bigger systems, I think code that focuses too much on "mechanism" becomes hard to read and hard to modify.
But again, nice solution!
41
u/RazarTuk Oct 16 '26 edited Oct 16 '26
Nice! Speaking as a more experienced engineer, that's more or less exactly what I would have done! Although instead of pushing and popping from a stack, I probably would have made two lists - one with all the opening brackets and one with all the closing brackets - reversed one, and made sure they line up. For example, for
([)], I'd see([and)], but reversing one, they'd look likewhich doesn't line up, so you know they're mismatched. (I think that algorithm works. Now I want to hop on Leetcode myself to test it)
EDIT: Nope. My algorithm doesn't work. It failed on
()[]{}, where it comparedEDIT: Oh, and I'm intentionally leaving this up as an example of "See? Experienced engineers can mess up too"
16
u/Stickhtot Oct 16 '26
You're one step closer to making a compiler!
4
u/likethevegetable Oct 16 '26
Good solution! Would recommend easier to follow variable names, and I tend to prefer continue after your if logic instead of using the else clause.
4
u/Tazwar89 Oct 17 '26
Can you (or anyone else) explain why the open brackets were numbered 1,2,3 and the closed brackets 7,8,9?
I'm also unable to decipher why you chose the conditional "if inputs[a]<4".
2
u/Mark3141592654 Oct 16 '26
I remember feeling great when I first solved this as well. Keep it up!
2
u/WingZeroCoder Oct 16 '26
Ok, that’s pretty fun. I’ve done this before with a sort of key / value map from open to closed character before (similar to another commenter here), but having them add to 10 is a pretty fun way to accomplish this.
2
u/JamzTyson Oct 19 '26
Well done.
As a critique, I'd say that I'm not keen on the magic numbers and logic disguised as math (for example:
if x+inputs[a]!=10:).Your code works and covers the edge cases, so my critique is more about style and maintainability than correctness. Personally I'd go for a more obvious abstraction such as:
I didn't use the
if(len(s)%2!=0):optimisation, but if you are expecting massive, odd-length string inputs, then it may be worth adding.1
u/spidermask Oct 17 '26
That's a pretty good solution!! And now I'm having Compiler course PTSD
1
u/Lumpi993 Oct 17 '26
Nice solution :) I have a small question regarding readability. Which of these is more readable?
Both are functionally the same, but the second one is less cluttered and, for me at least, communicates very well "if there's a remainder, do... ", but I'm really not sure. I also think the second one is more 'pythonic', but maybe there's an even better way.
1
u/marrsd Oct 17 '26
You did it like that! Very interesting. I have a challenge for you. Suppose you have to match multiple - or deeply nested - brackets of the same type. Would this solution work? Is there an easier solution?
On the question of if it would work, this is delving into the area of mathematical proof. That's something I want to get better at, so I will use this as an example to practise on. Thank you for your post :)
1
u/NadiaReadsChunks Oct 17 '26
stack approach is right, the 10 sum trick is cute but a dict of closers to openers reads way better. Would you still know what that 4 means in a month?
1
u/Poddster Oct 17 '26
Well done! For very long input strings, you're storing a large number of previous brackets. Do you need to do that? After all, the input string already has these brackets stored inside it , perhaps you don't need to store your own?
As a challenge, see if you can do it with 3 variables using a completely different approach.
1
u/SciNinj Oct 17 '26
Some general points: language is a tree; parsing a language tree top to bottom involves push-pop operations like a stack. So you just map opening characters with closing characters, and when you find an opening character you push the closing character to the stack. When you find the closing character you pop the stack.
1
u/captainAwesomePants Oct 16 '26 edited Oct 16 '26
I've never seen this particular take, and it's quite an elegant way to express that idea! Good job. I can tell you that it is NOT an intuitive jump for a lot of novices I've helped out that this problem should use a stack, and it's a good sign that you saw that and you also came up with a twist on what to store that helped you solve the problem. An excellent sign of really grasping the idea!
If you're curious, there are several other interesting ways to solve this problem. One way might be to map your inputs to their matching friend instead of to a number, like so:
You could also do the exact reverse of this and store the original character and use the input map to check which character is the correct match:
That's probably the one I would use.
Or, if you're a monster and just wanted to make things hard on everyone, you could do this:
0
u/Available_Safe1818 Oct 17 '26
'inputs' should map the character to its matching character, and you don't need then sum to 10 part.
-1
u/horenso05 Oct 16 '26
That is a very famous language called the Dyck language, basically you can map a Dyck language to any context free language (such as the syntax of say Java) by just choosing the degree (how many different parenthesis types) and what each symbol would map to. Your solution with the different types adding to 10 is very clever!