简体   繁体   中英

Find number of all cases of character array in Swift

I am trying to solve the problem of whether the parentheses in the array are closed correctly or not. For example, if two pairs of parentheses are closed correctly, there are two cases: "()()" "(())". I want to first find the number of all cases of 2 pairs of parentheses, and then verify that it is closed correctly or not.

What I'm thinking is that when I try to find 2 pairs, first of all, "(", "(", ")", ")" is to make all cases first with 2 open and 2 closed parentheses. How can I get all the cases with 2 open parentheses + 2 closed parentheses? Please help me.

In pseudo code:

unClosedCounter = 0

For char in string{
    if char == "("
    {
        unClosedCounter++
    }

    if char ==  ")"{
        if unClosedCounter == 0
        {
            return false
        }

        unClosedCounter--
    }
}

Return unClosedCounter == 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM