Input: arr[] = ["gfg", "for", "geeks", "geeks", "for"]
Output: 1
Explanation: After the first iteration, we'll have: [gfg, for, for]. Then after the second iteration, we'll have: [gfg]. No more eliminations are possible. Hence, the result is 1.
Input: arr[] = ["ab", "aa", "aa", "bcd", "ab"]
Output: 3
Explanation: After the first iteration, we'll have: [ab, bcd, ab]. We can't further destroy more strings and hence we stop and the result is 3.
Input: arr[] = ["tom", "jerry", "jerry", "tom"]
Output: 0
Explanation: After the first iteration, we'll have: [tom, tom]. After the second iteration: 'empty-array' . Hence, the result is 0.
The idea is to iteratively remove consecutive same words from the array until no adjacent duplicates remain. The thought process follows a linear scan, checking adjacent elements, and if found equal, they are removed. After removal, the pointer moves back one step to recheck the new adjacency, ensuring all duplicates are handled efficiently.
The idea is to use a stack to efficiently remove consecutive duplicate words while maintaining order. As we iterate through the array, we check if the top element of the stack matches the current word. If they are the same, we pop the stack, otherwise, we push the word.