Problem Statement
You are walking along a straight path. The path consists of consecutive unit-length segments. Along the path there may be some signal towers. Whenever you are standing on some segment of the path, you can receive a signal from each tower that is no more than maxDist segments away (either in front of you or behind you).
You are given the layout of the path in the
Return the maximum number of towers from which can you receive a signal at the same time.
Definition
- Class:
- LinearSignals
- Method:
- maxSignals
- Parameters:
- String, int
- Returns:
- int
- Method signature:
- int maxSignals(String towers, int maxDist)
- (be sure your method is public)
Constraints
- maxDist will be between 1 and 50, inclusive.
- towers will contain between 1 and 50 characters, inclusive.
- Each character in towers will be '.' or 'X'.
Examples
"..X.X.X.."
2
Returns: 3
If you stand on the segment that contains the middle tower, you can also receive a signal from the other two towers - each of them is exactly two segments away from you at that moment.
"..X..X.."
1
Returns: 1
Since you can only receive a signal from your segment and the two adjacent segments, there's no way to get signals from both towers at the same time.
"X...X....X.."
20
Returns: 3
Signals reach very far, so you can easily get all three towers at once. In fact, regardless of where you're standing, you are always getting the signal from all three towers.
"X...X....X.."
2
Returns: 2
"X...X....X.."
5
Returns: 3
".XX.X......X..X...XXX.....XX.XX...X.XXX..........."
7
Returns: 8
"..X..X..X..XX.............XX........XXXX..X......X"
3
Returns: 5
"X.X.XX..XXX....X.....X..X.X.XX....XX.XX.XX.X...X.X"
9
Returns: 10
".....XX.....XX...X.....X....XX..XX...XX.....X..X.."
3
Returns: 4
"X......X.....X....X.........X...X...X..X...X.....X"
1
Returns: 1
"XXXXX........."
2
Returns: 5
"........XXXXX"
2
Returns: 5
"....."
1
Returns: 0
Sometimes there are no signal towers at all.
"X.X...XXX..XX."
2
Returns: 3
"...X.X..."
1
Returns: 2
"X"
50
Returns: 1
"X........X..X..X."
2
Returns: 2
"X..X..X..X..X..X..X"
1
Returns: 1
"X...X"
2
Returns: 2