#C174. Remove Consecutive Duplicate Words
Remove Consecutive Duplicate Words
Remove Consecutive Duplicate Words
You are given a string consisting of multiple words separated by spaces. Your task is to remove any word that is immediately repeated. In other words, if a word appears consecutively, only the first occurrence should remain.
For example, given the input hello hello world
, the output should be hello world
. The same rule applies to any sequence of consecutive duplicate words.
Note: Words are case-sensitive, meaning that Hello
and hello
are considered different.
The problem can be formally stated as follows:
Given a string \( S = w_1 \; w_2 \; ... \; w_n \) where each \( w_i \) represents a word, construct a new string \( S' \) such that for every \( i (2 \leq i \leq n) \), if \( w_i = w_{i-1} \) then \( w_i \) is omitted from \( S' \).
inputFormat
The input consists of a single line containing a string. The string is made up of words separated by spaces. The input is provided via standard input (stdin).
outputFormat
Output a single line, via standard output (stdout), which is the modified string after removing all consecutive duplicate words.
## samplehello hello world
hello world