codedecks-in / LeetCode-Solutions

This repository consists of solutions to the problem from LeetCode platform. Subscribe to our Channel for more updates
https://www.youtube.com/c/codedecks
MIT License
831 stars 419 forks source link

Added a more efficient solution for the Valid_Parentheses LeetCode Problem #476

Open danieldotwav opened 9 months ago

danieldotwav commented 9 months ago

Pull Request Template

Description

A robust C++ solution for validating parentheses in strings. This project features a well-tested algorithm that handles different types of parentheses, including (), {}, and []. It is designed to cover a wide range of scenarios, from simple to complex nested structures, ensuring the correct ordering and pairing of parentheses.

Put check marks:

Have you made changes in README file ?

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Please also note any relevant details for your test configuration.

// Create a string for each test case
    std::string label, test_string;
    std::unordered_map<std::string, std::string> test_cases = {
        {"Test 1:", "()"},           // Simple valid case
        {"Test 2:", "{}[]()"},       // Mixed types, valid
        {"Test 3:", "(]"},           // Mismatched types
        {"Test 4:", "([)]"},         // Valid types but wrongly ordered
        {"Test 5:", "(((())))"},     // Nested, valid
        {"Test 6:", "("},            // Single, unmatched
        {"Test 7:", "){"},           // Invalid start with closing
        {"Test 8:", "(()){[()]}"},   // Complex, valid
        {"Test 9:", ""},             // Empty string
        {"Test 10:", "((()(())))"},  // More complex nested, valid
        {"Test 11:", "([{}])"},      // Mixed types nested, valid
        {"Test 12:", "(}"},          // Mismatched pair
        {"Test 13:", "([]{}"},       // Missing closing
        {"Test 14:", "([)]{}"},      // Mixed types with wrong order
        {"Test 15:", "((((((("},     // Multiple unmatched opening
        {"Test 16:", "))))))"},      // Multiple unmatched closing
        {"Test 17:", "([[[{{{{"},    // Multiple unmatched opening of different types
        {"Test 18:", "}]]]))"},      // Multiple unmatched closing of different types
        {"Test 19:", "(())(()"},     // Partially matched
        {"Test 20:", "[([])](){}"}   // Complex, valid with all types
    };

    // Access each test case and print the result
    for (const auto& pair : test_cases) {
        const std::string& label = pair.first;
        const std::string& test_string = pair.second;
        std::cout << std::left << std::setw(TEST_NUMBER_WIDTH) << label << std::setw(TEST_STRING_WIDTH)
            << test_string << std::setw(OUTCOME_WIDTH) << (isValid(test_string) ? "Valid" : "Invalid") << '\n';
    }

Make sure all below guidelines are followed else PR will get Reject:

welcome[bot] commented 9 months ago

I can tell this is your first pull request! Thank you I'm so honored. :tada::tada::tada: I'll take a look at it ASAP!