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
(2 votes)
Loading...

Similar Posts

Subscribe
Notify of
7 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Erzesel
10 months ago

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" )

 $ESC = [char]0x1b for ($red=0; $red -le 1000; $red+=10 ){ #ANSI-Escapesequenzen sind schon besonders :) "$ESC[48;2;$(($red+3)%255);2;0;38;2;128;255;0mIch bin mit RGB-Sequenzen gefaerbt :) Rotanteil:""$(($red+3)%255)""$ESC[0m" }

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

 /* lass uns über schließende Klammerzeichen: )]}> reden*/ System.out.println("Hallo Textsmilie :)"); // Kommentar :)

Javascript (RegEx patterns are not declared as "string"! —- really bad trap–)

 let text = "Ein Smilie :)"; let result = text.match(/:\)/i);

…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.

Suiram1
10 months ago

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

Suiram1
10 months ago
Reply to  Valentin2882

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?

iQa1x
10 months ago

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.

 char stack[50]; // maximale Klammerebenen int sp=0; // Stackpointer char[] marks = new char[]{'(',')','[',']','{','}'}; for (int i = 0; i < s.Length; i++){ for(int j = 0; j < marks.Length; j++){ if (s[i] == marks[j]) { if (j%2==0) { //öffnende Klammer stack[sp++]=marks[j+1]; // passende schließende auf den Stack } else { // schließende Klammer if (sp==0) return false; // keine öffnende da if (stack[--sp]!=marks[j]) return false; // falsche Klammer } } //if } //for } //for if (sp>0) return false; // mehr öffnend als schließend return true;

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.

Erzesel
10 months ago
Reply to  iQa1x

However, this does not work if solitary clamps are in strings/comments (always). …and almost every language has its very special rules …

KefuSH
6 months ago
Reply to  iQa1x

And then comes a comment with a smiley 🙂 and interprets it as a closing bracket