A canal lock keeper logs the direction of every vessel that passes through during a shift, writing one character per vessel in the order they passed: u for a vessel heading upstream, d for a vessel heading downstream. The lock's operating rule requires that, within any single shift, every upstream passage be logged before any downstream passage — once the first downstream passage is logged, no upstream passage may be logged afterward.
Given a shift's log, determine whether it obeys this rule.
A single line containing the shift log: a nonempty string made up only of the characters u and d.
Print true if no u appears anywhere after a d in the log (equivalently, the log consists of some number of u characters followed by some number of d characters), and false otherwise.
u or d.Example 1
Input
uuuddd
Expected
true
Explanation
All three upstream passages (positions 1-3) are logged before all three downstream passages (positions 4-6), so no downstream passage is ever followed by a later upstream one, and the output is true.
Example 2
Input
uduu
Expected
false
Explanation
A downstream passage is logged at position 2, but another upstream passage follows at position 3. This violates the rule that no upstream passage may occur after a downstream one, so the output is false.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →