import java.util.*;
class StackDemo {
public static void main(String[] argh) {
boolean flag = true;
String str = "(()){}()";
int l = str.length();
flag = true;
Stack<String> st = new Stack<String>();
for (int i = 0; i < l; i++) {
String test = str.substring(i, i + 1);
if (test.equals("(")) {
st.push(test);
} else if (test.equals("{")) {
st.push(test);
} else if (test.equals("[")) {
st.push(test);
} else if (test.equals(")")) {
if (st.empty()) {
flag = false;
break;
}
if (st.peek().equals("(")) {
st.pop();
} else {
flag = false;
break;
}
} else if (test.equals("}")) {
if (st.empty()) {
flag = false;
break;
}
if (st.peek().equals("{")) {
st.pop();
} else {
flag = false;
break;
}
} else if (test.equals("]")) {
if (st.empty()) {
flag = false;
break;
}
if (st.peek().equals("[")) {
st.pop();
} else {
flag = false;
break;
}
}
}
if (flag && st.empty())
System.out.println("true");
else
System.out.println("false");
}
}
Для итерации объекта вы можете использовать:
let record = {'key1': 'value1', 'key2': {'test':'123'}, 'key3': [{'test1':'23'}]};
if(typeof record === 'object' && record != null){
Object.keys(record).forEach((key)=>{
if(typeof record[key] == 'object' && record[key] != null){
//logic for the object
}else if(typeof record[key] == 'array' && record[key] != null){
//logic for array
}else {
//logic for other type
}
});
}else if(typeof record === 'array'){
record.forEach(r => {
if(typeof r == 'object' && r != null){
//logic for the object
}else if(typeof r == 'array' && r != null){
//logic for array
}else {
//logic for other type
}
});
}