Problem Statement
We are given a
Definition
- Class:
- SimplePath
- Method:
- trouble
- Parameters:
- String, int[]
- Returns:
- int
- Method signature:
- int trouble(String direction, int[] length)
- (be sure your method is public)
Constraints
- direction will contain between 1 and 50 characters inclusive.
- length will contain the same number of elements as direction has characters.
- Each character of direction will be an uppercase letter 'N', 'E', 'S', or 'W'.
- Each element of length will be between 1 and 10,000 inclusive.
Examples
"NWSEEN"
{5,5,3,2,5,2}
Returns: 0
The first segment goes north 5 units. The second then goes 5 west. The third segment goes 3 south. The fourth goes 2 east. The fifth segment continues east and touches (in fact intersects) the first segment. Return the zero-based index of the first segment.
"NWWS"
{10,3,7,10}
Returns: -1
This road is a 10 x 10 x 10 U-shaped simple path.
"NWES"
{2,2,1,2}
Returns: 1
Nothing touches the first segment, but the second segment touches the third segment. In fact, the third segment doubles back and covers half of the second segment. The zero-based index of the second segment is 1.
"NWSE"
{100,100,100,100}
Returns: 0
This road forms a square, so the last segment touches the first segment and we return the index of the first segment.
"NNNNNNNNNNNNNNNNNNNN"
{1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1}
Returns: -1
"NNNNNNNNNNNNNNNNNNSN"
{1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1,1,2,1,1}
Returns: 17
"ENWSE"
{5,2,19,2,19}
Returns: 0
"EN"
{5,2}
Returns: -1
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW"
{10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000,10000}
Returns: -1
"ESWNESWNESWNESWNESWNESWNESWNESWNESWNESWNESWNESWN"
{9950,9950,9951,9951,9952,9952,9953,9953,9954,9954,9955,9955,9956,9956,9957,9957,9958,9958,9959,9959,9960,9960,9961,9961,9962,9962,9963,9963,9964,9964,9965,9965,9966,9966,9967,9967,9968,9968,9969,9969,9970,9970,9971,9971,9972,9972,9973,9973}
Returns: -1
"ESWNESWNESWNESWNESWNESWNESWNESWNESWNESWNESWNESWN"
{9950,9950,9951,9951,9952,9952,9953,9953,9954,9954,9955,9955,9956,9956,9957,9957,9958,9958,9959,9959,9960,9960,9961,9961,9962,9962,9963,9963,9964,9964,9965,9965,9966,9966,9967,9967,9968,9968,9969,9969,9970,9970,9971,9970,9972,9972,9973,9973}
Returns: 39
"ENWNESW"
{20,20,20,20,5000,30,5000}
Returns: 1
"ENWNESWS"
{20,20,20,20,5000,30,5000,10}
Returns: 0
"ENWNESWS"
{20,20,20,20,5000,30,5000,5000}
Returns: 0
"NNWSENW"
{10,10,10,20,20,20,10}
Returns: 0
"NNWSENW"
{10,10,10,21,20,21,10}
Returns: 1
"NNWSENWE"
{10,10,10,21,20,21,10,20}
Returns: 1
"NNWSENW"
{10,10,10,21,20,21,30}
Returns: 1
"EEEEEW"
{1,1,1,1,1,10}
Returns: 0
"NENENWS"
{ 1, 1, 1, 1, 1, 1, 1 }
Returns: 2
"NWSE"
{ 100, 100, 100, 100 }
Returns: 0
"NWES"
{ 2, 2, 1, 2 }
Returns: 1
"EEEEEW"
{ 1, 1, 1, 1, 1, 10 }
Returns: 0
"NWSENW"
{ 4, 4, 2, 2, 1, 5 }
Returns: 2
"NWSEEN"
{ 5, 5, 3, 2, 5, 2 }
Returns: 0
"EEW"
{ 1, 1, 2 }
Returns: 0
"NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN"
{ 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000 }
Returns: -1
"SESWNW"
{ 5, 5, 5, 3, 8, 8 }
Returns: 0
"NS"
{ 1, 1 }
Returns: 0
"NENENENENENENENENENENENENENENENENENENENENENENENENE"
{ 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000 }
Returns: -1
"EW"
{ 2, 1 }
Returns: 0
"EW"
{ 1, 2 }
Returns: 0