Why doesn't the code for checking correct bracket placement work?
Hello,
I know I'm not that good. But I don't understand how to do this right. What am I doing wrong?
Please without changing everything, i.e. stack etc.
public class Solution { public bool IsValid(string s) { if (s.Length % 2 != 0) { return false; } char[] marks = new char[] { '(', ')', '[', ']', '{', '}' }; for (int i = 0; i < s.Length / 2; i++) { for (int j = 0; j < marks.Length; j++) { if (s[i] == marks[j] && s[s.Length - 1 - i] != marks[j + 1]) { return false; } } } return true; } }
It is an algorithm that checks whether parentheses have been placed correctly:
()[) -> false, ()[]([]) -> true
I do not want to specifically address your code, but to show general case knits that can bring down almost all "apparently" working solutions.
The network is full of quite good approaches which in ideal case I work quite well:
But there is Murphy's Law and there is nothing ideal. also applies to the text/code to be examined. Depending on the language of the code to be examined, you must exclude comments, string declarations
Here is a very nasty example of a power shell code to be examined: ( who lets you fly every simple "counter" )
The clamps are actually balanced. algorithm should be #Comment text , 'Literal' and "Stringdeclaration" exclude. But now it comes even thicker… within the string declaration I have also declared runtime functions $(Functionexecution) which, in extreme cases, could also contain interleaved claws. that would have to be taken into account again 🤮 I just chose Powershellcode as a text to drive the thing to the top.
Also texts to be examined in other programming languages contain comments and other constructs that would bring a primitive (interest) algorithm out of step:.
Java
Javascript (RegEx patterns are not declared as "string"! —- really bad trap–)
…at this point, a simple counter/stack would come out of
I don't know what you're going to do and how universally enforceable your algorithm should be?
I just wanted to show you that it is not easy to count the brackets up and down. Depending on the language, you also have to distinguish what functional claws are and which clamps are uninteresting.
I would put it like that the depth inside as many clamps you are looking (I know is not as good explained).
As a little more detailed:
I would pass through all signs in a foreach loop (clear also for) and define one int which indicates the depth (outside the loop declared). When an opening clamp is found, the increment is incremented int and decremented when the clamp is closed and at the end it is checked whether the int which indicates the depth is equal to 0. However, if it is different types of brackets, you need to make your own int for any type.
lg Suiram1
Thanks will try it 🙂
I missed something.
This would be covered: ()[)
However, something like this: (][) would be evaluated as true, although it is wrong, since there are equal numbers of opening and closing clips from any kind.
So you don’t get around a kind of stack. Do you have to try out the answer from @iQax? Why don’t you use a stack?
This doesn't work because you look out that the closing clamp is in the same position as the opening one, only "from the back". This is already happening ()[].
You have to remember the opening clip.
PS: Without warranty, there are certain syntax errors in it. Without a stack or you somehow remember the order of the clips it is not possible.
However, this does not work if solitary clamps are in strings/comments (always). …and almost every language has its very special rules …
And then comes a comment with a smiley 🙂 and interprets it as a closing bracket