Anyone seen this question before?
Anonymous User
323

There is a castle and it has two types of soldiers:
Left Soldier
Right Soldier

And there are two rules these soldiers need to follow:

Rule 1 -> Soldier left can only go left and Soldier right can only go right
Rule 2 -> When these soldiers meet they can't cross each other, meaning soldier left can only go up till soldier right's position and vice versa.

So you're given a String with Soldiers initial positons and another String with changed positions and you need to validate if this String follows the above rules.

Example 1
Initial String - "---L--R--"
Second String - "--L---R--"

Output - True
Explanation - L soldier moved to the left so they followed the rules

Example 2
Initial String - "---L--R--L--R--"
Second String - "----L-R--L--R--"

Output - False
Explanation - L soldier moved to the right so they broke Rule 1

Example 3
Initial String - "---L--R--"
Second String - "--R---L--"

Output - False
Explanation - Soldiers crossed each other so they broke rule 2

and you're given a method signature

public boolean isValid(String a, String b) {
	
}
Comments (2)