Compilers and parsers Problem Code: COMPILER

I tried in different ways to get the correct answer but EVERY TIME I GET WA, can anyone tell me where I am going wrong, I am really got stuck at this point and have no clue why my code is failing every time. Help would be much appreciated!!!

Here is my code:

Try <<>

1 Like

Its giving me output as 2 which seems to be correct. I don’t know at which Test case my code is failing.
@sebastian, do you know where iam going wrong?

How it is correct?
You have to find longest prefix

1 Like

<> in <<> , Am i wrong here in understanding question? if iam wrong can you please elaborate?

You have to start from the first index

1 Like

I didnt get you, according to what i understood in a string given for eg: <<> have to search for opening and ending brackets.
Is it like we have to match β€œ<” and β€œ>” since starting index and checking how many matches with it?
for the ex you have given: <<> output should be 0?
<<>>>>><<<<> β€”> output should be 4?
Am i right?

You have to find correct match from the starting index. you should tell the length of the longest prefix of each of these expressions that is valid, or 0 if there's no such a prefix. Prefix starts from the first index only.

Both outputs are correct (0 and 4).

Hope this cleared your doubt

1 Like

Thanks alot!!! I understood the code wrongly and wondering for which TC it is failing.