Data Structures & AlgorithmsVery hardTCS NQT · Infosys
Repeatedly delete any two adjacent equal characters from the string abbacbaca until no such pair exists. How many characters remain?
- A. 5
- B. 6
- C. 4
- D. 7
Correct answer: A. 5
A stack handles this in one pass: push each character unless it equals the top, in which case pop. The final stack size is the answer, and the order of deletions does not matter.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- How many substrings of length exactly 4 of the string abbaccddcc are palindromes?
- What is the sum, over all contiguous windows of length 3, of the number of distinct values in the window, for the array [1, 2, 1, 3, 4, 2…
- How many index pairs i < j in the array [12, 21, 3, 30, 111, 9, 18] have numbers with equal digit sums?
- How many index pairs i < j in the array [1, 2, 3, 4, 6, 7, 8, 9] have a[i] a[j] mod 5 = 1?
- How many contiguous subarrays of length 3 in the array [1, 2, 3, 1, 2, 3, 3] have all three elements distinct?
- How many contiguous subarrays of [1, 2, 3, 4, 5] have a bitwise XOR less than 4?