/ Published in: C++
leetcode.com
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/** \file Valid Parentheses.cpp * \details * Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. * The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. * \author LOC */ #include <iostream> #include <cstring> using namespace std; //! \brief check string is valid? return true if valid otherwise false //! \param string[] a string which entered from keyboard //! \return true if valid and false if invalid bool Solution(char string[]){ int length = strlen(string); for(int i=0; i<length; i+=2){ if((string[i]=='[' && string[i+1]==']') || (string[i]=='{' && string[i+1]=='}') || (string[i]=='(' && string[i+1]==')') ) return true; else return false; } } int main(){ char string[10]; cout < "Enter a string"; cin.getline(string,10); if (Solution(string)) cout << "Your string is valid"; else cout << "Your string is invalid"; return 0; }