- Related Questions & Answers
- Program to check robot can reach target position or not in Python
- Program to check person can reach top-left or bottomright cell avoiding fire or not in Python
- Program to check whether we can take all courses or not in Python
- Program to check whether we can unlock all rooms or not in python
- Program to check we can cross river by stones or not in Python
- Program to check we can update a list index by its current sum to reach target or not in python
- Program to check whether we can reach last position from index 0 in Python
- Program to check whether we can get N queens solution or not in Python
- Program to check we can visit any city from any city or not in Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check two rectangular overlaps or not in Python
- Program to check we can spell out the target by a list of words or not in Python
- Program to check we can find four elements whose sum is same as k or not in Python
- Program to check whether we can make group of two partitions with equal sum or not in Python?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Suppose we have a string containing letters of three types, R, B, and dot(.). Here R stands for our current position, B stands for a blocked position, and dot(.) stands for an empty position. Now, in one step, we can move to any adjacent position to our current position, as long as it is valid (empty). We have to check whether we can reach either the leftmost position or the rightmost position.

So, if the input is like s = "...........R.....BBBB.....", then the output will be True, as R can reach left most position, because there is no block.

To solve this, we will follow these steps −

- r_pos := index of 'R' in s
- return True when 'B' is not present in s[from index 0 to r_pos-1] or 'B' not in s[from index r_pos to end]

Let us see the following implementation to get better understanding −

class Solution: def solve(self, s): r_pos = s.find('R') return not 'B' in s[:r_pos] or not 'B' in s[r_pos:] ob = Solution() s = "...........R.....BBBB....." print(ob.solve(s))

"...........R.....BBBB....."

True

Advertisements