Let hope that someone will not rep me - again for dropping solution
Anyway it was pleasure doing this one :)
public class parenthesis {
public static void main(String[] args)
{
//Next ones are several strings just for testing. Only one should be active in a time
String s="{([]{()})}"; //should be true
//String s="{[]}(}"; //Should be false
//String s="[]{{{}"; //Should be false
//String s="[]{{}{}}"; //Should be true
//String s="(((})))"; //Should be false
//String s="((({})))"; //Should be true
//String s="{{{}}}}"; //should be false
String [][] par=new String [s.length()][2]; //Make array with length of a string s and 2 spare position for parenthesis and used/unused state
boolean valid=true; //Lets be positive from start...
for (int i=0;i<s.length();i++) //First will fill array with parenthesis
{
par[i][0]=s.substring(i, i+1);
par[i][1]="0"; //means not yet used
}
for (int i=0;i<s.length();i++)
{
//Check [ parenthesis
if (par[i][0].equalsIgnoreCase("]")) //Checking for [ ]
{
//means there is closed parenthesis.. check previous if there is opened one of same type and not already used
//also check if previous is opened one but from other type, this is also invalid
for (int j=i-1;j>=0;j--)
{
if ((par[j][0].equalsIgnoreCase("[") || par[j][0].equals("{")|| par[j][0].equals("(")) && par[j][1].equals("0")) //Check if previous parenthesis is open one of any type and not used one
{
//OK, i have open parenthesis... now check type. Valid is only if opened one match with closed one...
if (par[j][0].equals("[")) //If previous is open and is right type then
{
//OK, i need this type...
par[j][1]="1"; //MAke it used
par[i][1]="1"; //Make closed parenthesis used
//I …