Mr.Z's Challenge

3.3

33 votes
Easy
Problem

VNIT has recently built a huge skyscraper in the campus. Mr.Z is standing on the terrace of CSE Department from where the skyscraper is clearly visible. The skyscraper has a lift to go up and down. When Mr.Z looks at the building, the lift is at a random floor. Mr.Z starts noting down how the lift is going up and down. When the lift halts at the end of the day, Mr.Z is able to tell the difference between highest & the lowest floor the lift traverses related to the initial floor where the lift was. Mr.Z bets that nobody else can do this as this is a tough task. Do you think you can do it?

Given a string containing letters of 'U' & 'D' where 'U' denotes the lift going upwards and 'D' denotes the lift going downwards, find the difference between highest & the lowest floor the lift traverses related to the initial floor where the lift was.

INPUT:
The first line of input contains the number of test cases T. T lines follow. Each line contains a string of characters 'D' & 'U'.

OUTPUT:
For each test case output the required answer.

CONSTRAINTS:

  • 1<=T<=1000

  • 1<=Length of string<=105

Sample Input
1
UD
Sample Output
1
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Here, the lift goes up first from the initial position & next time it comes down again to the same position. Hence the total difference is 1.

Contributers:
Editor Image

?