// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 Longest Valid Parentheses Solution
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef LONGEST_VALID_PARENTHESES_H
#define LONGEST_VALID_PARENTHESES_H

#include <string>
#include <vector>
#include <stack>
#include <algorithm>

/**
 * @class LongestValidParentheses
 * @brief Provides multiple algorithms to find the length of the longest valid parentheses substring
 * 
 * This class implements three different approaches:
 * 1. Dynamic Programming approach - O(n) time, O(n) space
 * 2. Stack-based approach - O(n) time, O(n) space  
 * 3. Two-pass counting approach - O(n) time, O(1) space
 */
class LongestValidParentheses {
public:
    // LIFECYCLE
    LongestValidParentheses() = default;
    ~LongestValidParentheses() = default;
    
    // Copy constructor and assignment operator
    LongestValidParentheses(const LongestValidParentheses& other) = default;
    LongestValidParentheses& operator=(const LongestValidParentheses& other) = default;
    
    // Move constructor and assignment operator
    LongestValidParentheses(LongestValidParentheses&& other) noexcept = default;
    LongestValidParentheses& operator=(LongestValidParentheses&& other) noexcept = default;

    // OPERATIONS
    /**
     * @brief Find longest valid parentheses using dynamic programming
     * @param s Input string containing parentheses
     * @return Length of longest valid parentheses substring
     */
    int FindLongestValidDp(const std::string& s);
    
    /**
     * @brief Find longest valid parentheses using stack approach
     * @param s Input string containing parentheses
     * @return Length of longest valid parentheses substring
     */
    int FindLongestValidStack(const std::string& s);
    
    /**
     * @brief Find longest valid parentheses using two-pass counting (space optimized)
     * @param s Input string containing parentheses
     * @return Length of longest valid parentheses substring
     */
    int FindLongestValidTwoPass(const std::string& s);
    
    /**
     * @brief Default method - uses the most efficient approach
     * @param s Input string containing parentheses
     * @return Length of longest valid parentheses substring
     */
    int FindLongestValid(const std::string& s);

private:
    // HELPERS
    /**
     * @brief Validate input string contains only parentheses
     * @param s Input string to validate
     * @return true if valid, false otherwise
     */
    bool IsValidInput(const std::string& s) const;
};

#endif // LONGEST_VALID_PARENTHESES_H