Skip to content

Bug Report for validate-parentheses #4973

@francesco-puglisi

Description

@francesco-puglisi

The following C# solution to the Valid Parentheses question causes a compilation error on the web editor, but it compiles fine on Rider.

using System.Collections.Generic;

public class Solution {
    public bool IsValid(string s) {
        var stack = new Stack<char>();
        foreach (var c in s)
        {
            if (IsClosingBracket(c))
            {
                var previous = stack.Pop();
                var expectedBracket = GetCorrespondingOpeningBracket(c);
                if (!expectedBracket.HasValue) return false;
                if (previous != expectedBracket.Value)
                {
                    return false;
                }
            }
            else 
            {
                stack.Push(c);
            }
        }
        return stack.Count == 0;
    }

    private char? GetCorrespondingOpeningBracket(char c)
    {
        return c switch {
            ')' => '(',
            ']' => '[',
            '}' => '{',
            _ => null
        };
    }

    private bool IsClosingBracket(char c)
    {
        return c switch
        {
            ')' => true,
            ']' => true,
            '}' => true,
            _ => false
        };
    }
}

causes the following compilation error

MSBuild version 17.7.1+971bf70db for .NET
Determining projects to restore...
Restored /box/Main.csproj (in 36 ms).
/box/Main.cs(0,22): error CS1513: } expected [/box/Main.csproj]
/box/Main.cs(1,5): error CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarations [/box/Main.csproj]
/box/Main.cs(48,5): error CS8803: Top-level statements must precede namespace and type declarations. [/box/Main.csproj]
/box/Main.cs(48,5): error CS0106: The modifier 'public' is not valid for this item [/box/Main.csproj]
/box/Main.cs(69,5): error CS0106: The modifier 'public' is not valid for this item [/box/Main.csproj]
/box/Main.cs(80,5): error CS0106: The modifier 'public' is not valid for this item [/box/Main.csproj]
/box/Main.cs(87,1): error CS1022: Type or namespace definition, or end-of-file expected [/box/Main.csproj]

Build FAILED.

/box/Main.cs(0,22): error CS1513: } expected [/box/Main.csproj]
/box/Main.cs(1,5): error CS1529: A using clause must precede all other elements defined in the namespace except extern alias declarations [/box/Main.csproj]
/box/Main.cs(48,5): error CS8803: Top-level statements must precede namespace and type declarations. [/box/Main.csproj]
/box/Main.cs(48,5): error CS0106: The modifier 'public' is not valid for this item [/box/Main.csproj]
/box/Main.cs(69,5): error CS0106: The modifier 'public' is not valid for this item [/box/Main.csproj]
/box/Main.cs(80,5): error CS0106: The modifier 'public' is not val...
output limit exceeded

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions