Problems
Sequence of brackets
Тесты совсем от другой задачи что ль?
Во всяком случае сейчас с тестами все в порядке
{({)}} try this one too if you wanna find out why your program is wrong
import java.util.*;
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in);
// Read the input string
String sequence = scanner.nextLine();
// Check if the sequence is correct
String result = isCorrectSequence(sequence) ? "yes" : "no";
// Print the result
System.out.println(result);
}
public static boolean isCorrectSequence(String sequence) {
Stack<Character> stack = new Stack<>();
// Iterate through each character in the sequence
for (char c : sequence.toCharArray()) {
if (c == '(' || c == '[' || c == '{') {
// Push opening brackets onto the stack
stack.push(c);
} else if (c == ')' || c == ']' || c == '}') {
// Check for matching closing brackets
if (stack.isEmpty()) {
return false; // Unmatched closing bracket
}
char top = stack.pop();
// Check if the top of the stack matches the current closing bracket
if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) {
return false; // Mismatched brackets
}
}
}
// If the stack is empty, all brackets were matched
return stack.isEmpty();
}
}