Problem Statement
You are traversing a dangerous section of a video game level. The section contains some solid terrain blocks (denoted '-') and some blocks with spiky traps (denoted '*'). If you step onto a trap, you die.
You are given the layout of the level in the
Your task is to reach the block at the opposite end of the level. This block is also guaranteed to be solid.
You can move in three ways:
- Take a step (denoted 'S'): move one block to the right.
- Make a hop (denoted 'H'): hop over one block and land two block to the right of your current block.
- Make a jump (denoted 'J'): jump over two consecutive blocks and land three blocks to the right of your current position.
For example, if the level is "----", you can traverse it by making three steps (denoted "SSS"), a hop followed by a step ("SH"), a step followed by a hop ("HS"), or a single jump ("J").
If the level is "-**-", the only way to traverse it is by making a jump - remember that you cannot step onto the spiky traps.
In the above way, we can describe any way of traversing the level as a string in which each character is 'S', 'H', or 'J'. Two ways of traversing a level are considered different if they are described by different strings. For example, there are four ways to traverse the level "----", one way to traverse the level "-**-**-", and no ways at all to traverse the level "-*-****-*-".
Let W be the number of different ways in which we can traverse the level described by the
Definition
- Class:
- StepHopJumpMedium
- Method:
- countWays
- Parameters:
- String
- Returns:
- int
- Method signature:
- int countWays(String level)
- (be sure your method is public)
Constraints
- level will contain between 2 and 200 characters, inclusive.
- Each character of level will be either '-' or '*'.
- The first and the last character of level will be '-'.
Examples
"----"
Returns: 4
The first example from the problem statement.
"-**-"
Returns: 1
The second example from the problem statement.
"-*-****-*-"
Returns: 0
The third example from the problem statement: there are no solutions.
"------------------------------------"
Returns: 132436845
This level consists of 36 dashes. It can be traversed in many different ways. One of them is taking 35 steps, another is taking 11 jumps and then a hop. There are exactly 1,132,436,852 different ways in total. Remember that you should return this value modulo 10^9 + 7.
"--"
Returns: 1
"-*-"
Returns: 1
"--*-"
Returns: 2
"-*--"
Returns: 2
"--*-**-*-**-*-*-**-*-*-**--*-**----*-*-**-**--*-**-*---**-**-*-*---**-*-**-**-*-**-*-**-**---**--"
Returns: 864
"--*--*-**-*-**--"
Returns: 5
"-*--**--**-*--*-**--*-**-*-**-*--**-*-*-**-**--*-*-**-*-*-**-**--**--**--*-*-*-**-**--*---**-**---*-**-**-**-*-*-*--**-*-**-**-**-**-*-**-**-*--"
Returns: 5760
"-**---*-*--"
Returns: 6
"---*-----**-**-*-**-*--*-*--**--*--*--*----**-**-*--*-**-*-**-**--**----**-*-**--**--**--*----*--**-*--**-**-**--**-**-*-**-**--**-*-*-**---*-*-*-*-**-*-**---**--*-**-*--*-**-**--*--"
Returns: 766713600
"-*-**---*-*--*--**--**-*-*-*-**-**-**-**-**--**--**-*--*-**-*-*--**--**-**-**-**-*-**-**-*-**-*-*-**--**-**---**-*-*-*-**-*---**-**-*-*--*-**--*-**-*-*--*--*-**-*-*-*-*-**---**-*---*-*--"
Returns: 518400
"-*-**-*-*--*---*-*-*-*--**-**-**-*-*-**--**-**-*-**-**-**-**-**-**-*-**-**--**-**-*---**--**--*-*---*--**-*--**--**--**-*--*-*-**-*-**-*--"
Returns: 14976
"-**-*---*-*-**--**--**-*-*-*-**--*--"
Returns: 15
"-**-*-*-**--*-*---**-*-*-**-**--*-*--*--*-**--**--**-**--**-*-**-*-**-**--**--*-**--*-**--*-**-*--**-*-**--**-**-*--*---**------**--**-----"
Returns: 1118208
"-**-*-*--*-*-**-**--**-*--**-*----**--"
Returns: 36
"-*--*-**-**-*-**-**-**-**-*-**-**-*-**--"
Returns: 3
"-*-**-*-**-*---*--**-*--*-**---*-*-**--*--**-*-*-**-**-*-**-**----**--**-*-*--**--*---**-**-*--**-**-**--*-*--**--*--"
Returns: 207360
"---*-**--**-*-**-**-*-*--*-*-*-*-*--**-**-**-**-*--*-*---*--*-*-**--*-**-**-**-*-*---*-*-----*-*---"
Returns: 358020
"---**-**--*-**--**-*-*--*-*--**-**-**-*-*-**-*----*--**--**-**---**-**--*-*---*--"
Returns: 11520
"-**-*-**-*---*-**-**-*--**-*-*-*-**-**-*-**--*-*-*--**-*-*--*---**-**-**---**----*-*-*-**-*-**-**---**-**-**-*-**-**-*-*-**-**----**--**-**------**--**-*-**-**-*-**-**-*-**-**--"
Returns: 399360
"--**-**-**--"
Returns: 1
"-**-----*-**-*-**-*-*-**-*-*-*-**---*-*-*-**-*-*-*-**-*-*-*-*--**-**-*--*-*--**--**--*-**-*-**-**-**---*-*--**--*-*-**-*--*-*-*-*-**---**--**--*-**--*-**-**-*--**-*---"
Returns: 1368576
"--**--**-**-**-*-**-**-**--**---*-*--*-**--"
Returns: 9
"---*-**-**--*-*-**-**--*---**-**-**-**-**--*-**-*--*-**-*--*-*-**-**--*-*-**-**-**-**-*-**--**--*-*-*---**--*-*-*--*---*-**-**---**-*-*--*-**--**-**---*-*--*----**-**-**--**--**-*-*-*-*---**--"
Returns: 145566720
"-*---**--**-**-*-**-**-*---*-*-**-**-**-*-*-**---*-*-**--**-**-*-*-*-*-**--**-**-**--"
Returns: 45
"-*-**-**--**------**-**-**-**-*-**-**-*--*-**-**-**-**-*-**--**-*-*---**-**-*---*-*-**-*-*-**--**-**-*-**-*-*-*-**--**-*--*-**-*-*-*-*----*--*---**-**-*--*--**-*-*-**-*--"
Returns: 1105650
"--*-*---**--**-*-*----*-**-*-**-**--**--**-*-**-*-**----*-**-*--**-**-*-**-**-*-*-*-*-**---*-----**-**-**-*-**-**--*-**-**-**-**-**--*-**---"
Returns: 150336
"-**--*-*--**-----**--**-**-**-**-**--*--*---*-*--*--**-*-**-**----**-**--"
Returns: 11760
"-**-*-*-**-**-*--*--**--*--*-**-*--*--*-**--*-**----**---*-**-*-**--"
Returns: 4800
"-**-*-**--*---**-*-*--*-*---**-*-*-*-*-**-**--*--**--**-**-*-**-**-*--*-*-**-**--"
Returns: 405
"-**-**-*-**-*-**--*-*-*-*--*-*---**-*----*-**--*-*-**-*-**--*--"
Returns: 972
"-**----**-*-*--**-*-*--"
Returns: 16
"-**-*---*-*--**--**-*--*-*-**-**-**--**-*---*--*-*--*-**-*--*-**-----*-**---"
Returns: 77220
"-*---**-**-----*-*-*-**-*--*-*-*---*-*-**--**-**-**---**-*-**-**-**---**---**-*--**-*---*-*--*--*-**-**---*-*--*--*-**-**---"
Returns: 15206400
"--*--**---*-**--**-*-*-*-*-**-**-**-**-**-**-**-**-**-*-**--**----*-**-*-*-*-**-*--*-*--**-**-*-**-**--**-*-**-*-*-----**-*-**-*--"
Returns: 7128
"-*-**-*--**-**-**-----*---**--*-**-**-*-**--**-**--"
Returns: 116
"---*-*-**-**-**-*--*-*--**-**-*-**-*-*-**-**-*--*--**-----*-*--**-**-**-**-**--**-**-*--**-**--**--*-*-*--**-*-**-**-**--**-**-*-**--*-**-*--"
Returns: 63360
"-**----*--**-**-**--**-*--*-**-**-*-**--"
Returns: 30
"-**-*-**---**-*-*--**---**-**---*--**--**---*-*-*-*--**-**--*-*--*-**-*-*---*-*-*--*-**--*-**--**-**--*---**-**---*-**-**--*--"
Returns: 1944000
"-**-**-**-**--*----**-**-*--*-*--*-**-**-*---*-*-**-*-**--**-*---*-**--**---------*---**-*-**----*-**-**--**--*----*--*-**----**-**-**-*-**-**-**--*--*-*---*---"
Returns: 472399678
"--**-*-*-*-**-*--*-**-**-**-*-**--**-**---**--**--"
Returns: 6
"--*-*-*--**--*-**-*---**-*--*-*-**--**--**-*-*-**-*--*-*-**-*--*--**-*-*---*-----*--*-*--**--**--**-*-*-**--*-*-*-*---**-*--"
Returns: 5002560
"-*-**-**-*-**-*-**-*--*--*--"
Returns: 13
"-**-**-*--**-----*-**-*-*----**-*-**-**--**--*-*--*----**--**-**-**--**--*-*-*--**-**-**-**-*-**--**--**-**-**-*--"
Returns: 33792
"-*-**-*-**-**-*-*--*--**-**-*-*-**-*-**-**---*-**--*--**-*-*----**-*-**--**---*---*-**-*--*-**--**-**-*----*-**--*---**-**-*-*-----*-*-**-*-**-*-*-*---"
Returns: 24166350
"---*-**-**-**-*--**--**---**-----**-*---**---**--"
Returns: 504
"-**-*-*-*-**--*-**-*--**--**-*----*--*-*---**-*--*--*-**-**-*-**-**-*--*--**-*-**-**--**--**--"
Returns: 11520
"-*---**-*--**--*--*-**-**--*---*-*-**--*--*-----*--*--**---**-**-**-*-*-*-*-*-*-*-**---*-**-**-**-*-*-*-*--**-**-**-**--*--"
Returns: 2704320
"-**--*-*-**--*----*-**-*-**--*--*---*-**-**-**---*-**--**-**-*-**-**-*-*-**-**-*-**--**-**-----*-**-*-*-**-**--"
Returns: 20790
"--**-**-**--"
Returns: 1
"-**-*-*-*-*-**--*-**-**-**--**-**-**--*-*--**-*-*-**-**-*-**-**-**-**-**-*-**-**-**--*-*-*-*-**-**--"
Returns: 16
"-*---*-*-**-**-*--*-*--**-**-*-**-*-**-**---**----*-**-*-*---**-**-*--*--**--*---*----*--*--**-**-**--*-**---------**-**-**--*-**-**-*-*--**---**-**--**-*--*-**-*-*-*--*--*--"
Returns: 511964282
"-**--**-*-*-**-**--**-**--*---**-**-**-**---**--**-*-**--**--**---**-*-*--**-**--**-**--**-**-*-*-*-*-*-*--*-*-*-*-**-**--**-*-**-*-*-*---**---**-*-**-*-**-**-*-*--"
Returns: 1440
"-**--**--**--*--*-*-**-*-**-*-**-*-------*-*-**-*--*-**-*-**-*---*-**----*--**-**-**-**-**--"
Returns: 42750
"----**----*-*-**---**--*-**-*---*--**-*-**-**-*-*-**---**--*-**-*-*---**-**---**-*-*-*-**-**--**-**-*-**-*---**-**-**---*-**-*--"
Returns: 331776
"-*-**-*-**-**-*-*--*--**-**-*-*-**-*-**-**---*-**--*--**-*-*----**-*-**--**---*---*-**-*--*-**--**-**-*--*-*-**--*---**-**-*-*-----*-*-**-*-**-*-*-*---"
Returns: 8055450
"--*--**---*-**--**-*-*-*-*-**-**-**-**-**-**-**-**-**-*-**--**----*-**-*-*-*-**-*--*-*--**-**-*-**-**--****-**-*-*-----**-*-**-*--"
Returns: 0
"-*---**-*--**--*--*-**-**--*---*-*-**--*--*-----*--*--**---**-**-****-*-*-*-*-*-*-**---*-**-**-**-*-*-*-*--**-**-**-**--*--"
Returns: 0
"-**----*--**-**-**--**-*--****-**-*-**--"
Returns: 0
"-**----*--**-*****--**-*--*-**-**-*-**--"
Returns: 0
"-**----*--**-**-***-**-*--*-**-**-*-**--"
Returns: 0
"-**-**-*--**---*-*-**-*-*----**-*-**-**--**--*-*--*----**--**-**-**--**--*-*-*--**-**-**-**-*-**--**--**-**-**-*--"
Returns: 9216
"-**-*-**--*---**-*-*--*-**--**-*-*-*-*-**-**--*--**--**-**-*-**-**-*--*-*-**-**--"
Returns: 135
"-***-**-**--"
Returns: 0
"-**-***--*-*-**-**--**-*--**-*----**--"
Returns: 0
"-**----*--*****-**--**-*--****-**-*-**--"
Returns: 0
"-**----*--*****-**--**-**-****-**-*-**--"
Returns: 0
"-**-*-*-*-**--*-**-*--**--**-*----*--*-*---**-*--*--*-*****-*-**-**-*--*--**-*-**-**--**--**--"
Returns: 0
"-**-**-**-**-----*-**-*-*----**-*-**-**--**--*-*--*----**--**-**-**--**--*-*-*--**-**-**-**-*-**--**--**-**-**-*--"
Returns: 16896
"-*--**--**-*--*-**--*-**-*-**-*--**-*-*-**-**--*-*-**-*-*-**-**--**--**--*-*-*-**-**--*---**-**---*-**-**-**-*-*-*--**-*-**-**-**-**-*-**-**-**-"
Returns: 2880
"---*-*---**-**-*-**-*--*-*--**--*--*--*----**-**-*--*-**-*-**-**--**----**-*-**--**--**--*----*--**-*--**-**-**--**-**-*-**-**--**-*-*-**---*-*-*-*-**-*-**---**--*-**-*--*-**-**--*--"
Returns: 237945600
"---***-**-**-**-*--*-*--**-**-*-**-*-*-**-**-*--*--**-----*-*--**-**-**-**-**--**-**-*--**-**--**--*-*-*--**-*-**-**-**--**-**-*-**--*-**-*--"
Returns: 0
"-*--**--**-*--*-**--*-**-*-**-*--**-*-*-**-**--*-*-**-*-*-**-**--**--**--*-*-*-**-**--*---**-**---*-**-**-**-*-*-*--**-*-*****-**-**-*-**-**-**-"
Returns: 0
"--*-**-*-****-*-**-*-*-**--*-**----*-*-**-**--*-**-*---**-**-*-*---**-*-**-**-*-**-*-**-**---**--"
Returns: 0
"-*--**--**-*--*-**--*-**-*-**-*--**-*-*-**-**--*-*-**-*-*-**-**--**--**--*-*-*-**-**--*---**-**---*-**-**-**-*-*-*--**-*-**-**-**-**-*-**-*****-"
Returns: 0
"--------------------------------------------------------*--------------*------------------------------------"
Returns: 332479209
"------------------------------------------------------------------*---------------------*-------------------------------------------*----------------------------------------"
Returns: 293754738
"-------*---*------------------------------------------------------------------------------------------------*-----------------------------------------"
Returns: 83890944
"----------------------------------------------------------------------------------------------------"
Returns: 92295268
"---*----------------------------------------------------------------------------------------------------------------------------------------*--------------------"
Returns: 190898320
"-----------------------------------------------------------*--------------------------------------------------*-------------------------------------------------------------"
Returns: 502528645
"------------------------------------------------------------------------------------------------------------------------------------*---------------------------------"
Returns: 609401982
"---------*----------------------------------------------------*---------------------------------------------------------------------------------------------"
Returns: 119249972
"-------------------------------------------------------------------------------------------*--------------------------------------------------------------*---------------------------------"
Returns: 790430242
"---------------------------------------------------*------------------------------------------------------------------------------------------------*----------------------"
Returns: 189005554
"-----------------*--------------------------------------------------------------------------------------"
Returns: 548851426
"-----------------------------------------------*---------------------------*------------------------------------"
Returns: 822088685
"-------------------------*---------------------------------------*-------------------------------------------------------------------------*-------------------------"
Returns: 586085211
"--------------------------------------------------------------------------------------*------------------------------------------------------------------------------------------------------"
Returns: 114067698
"-----------------------------*----------------------------------------------------------------------------------------------------------------------------------"
Returns: 198450281
"--------------------------------------------------------------------------*----------------*---------------------------------------------------------------------------------*---"
Returns: 644348489
"-------------------------------------------------------------------*-------------------------------------------------------------------------------------------------------"
Returns: 958976168
"-----------*-------------------------------------------------------------------------------------------------------------------------------------*----------------------"
Returns: 183897296
"-------------------------------------------------------------------*----------------------------------*------------------------------*-----"
Returns: 573377891
"------------------------------*--------------------------------------------------------------------------------------------------------------------------------------------------"
Returns: 346763564
"---*-*--*---*----*------------------------------------------------------------------------------------------------------------------------------------------------------------------**--**---**----**---"
Returns: 234580825
"------------------------------------*------------------------------------**---*------*-----*-------------------------------------*-"
Returns: 28396614
"------------------------------------*------------------------------------**---*------*-----*--------------------------------------"
Returns: 129703796