Created
October 22, 2017 15:13
-
-
Save krishna2nd/f3ff49874e8ea31a1e8a813a106cdf42 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parentheses pairing ({}[]()<>) | |
// | |
package main; | |
import ( | |
"fmt" | |
"strings" | |
) | |
func main() { | |
fmt.Println(stringMatch("{[(])}")); | |
} | |
func stringMatch(input string) bool { | |
var pushChars, popChars string = "{[(<" ,"}])>"; | |
var stack []rune | |
var pop rune; | |
for _, r := range input { | |
if strings.ContainsRune(pushChars, r) { | |
stack = append(stack, r); | |
} else if strings.ContainsRune(popChars, r) { | |
if (len(stack) > 0) { | |
pop = stack[len(stack)-1:len(stack)][0]; | |
k := pushChars[strings.IndexRune(popChars, r)] | |
fmt.Println(k, pop, r) | |
if (rune(k) == pop) { | |
stack = stack[: len(stack)-1] | |
} else { | |
return false; | |
} | |
} else { | |
return false; | |
} | |
} | |
} | |
return len(stack) == 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment