You are viewing a single comment's thread. Return to all comments →
JAVA 8
public static String isBalanced(String s) { String response = "YES"; String[] values = s.split(""); String listExpected = ""; String passValues = "({[";
for(int i = 0; i < values.length; i ++){ String value = values[i]; if(!passValues.contains(value)){ String[] list = listExpected.split(""); if(list[0].equals(value)){ listExpected = listExpected.replaceFirst("\\"+value, ""); } else { response = "NO"; break; } } if(value.equals("(")){ listExpected = ")" + listExpected; } if(value.equals("{")){ listExpected = "}" + listExpected; } if(value.equals("[")){ listExpected = "]" + listExpected; } } return response; }
Seems like cookies are disabled on this browser, please enable them to open this website
Balanced Brackets
You are viewing a single comment's thread. Return to all comments →
JAVA 8
public static String isBalanced(String s) { String response = "YES"; String[] values = s.split(""); String listExpected = ""; String passValues = "({[";