My issue
I’ve produced the correct result (with the sample input), but it says wrong answer.
I can clearly see the output matching sample output. Could this be some usabilty issue?
My code
import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner scanner = new Scanner(System.in);
Integer numTestCases = scanner.nextInt();
List<String> list = new ArrayList<>();
for (int i = 0; i < numTestCases; i++) {
list.add(scanner.nextLine());
}
Stack<Character> stack = new Stack<>();
list.forEach(str -> {
int numPair = 0;
for (int i = 0; i < str.length(); i++) {
char current = str.charAt(i);
if (stack.empty()) {
stack.push(current);
} else {
char previous = stack.pop();
if (previous == current) {
stack.push(current);
} else {
numPair++;
}
}
}
System.out.println(numPair);
});
}
}
Learning course: Greedy Algorithms
Problem Link: Chef and String Practice Problem in Greedy Algorithms - CodeChef