
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to check we can reach leftmost or rightmost position or not in Python
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 −
Example
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))
Input
"...........R.....BBBB....."
Output
True
- Related Articles
- Program to check we can reach at position n by jumping or not in Python
- 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 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
- Python program to check whether we can pile up cubes or not
- 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 form array from pieces or not in Python
- C++ code to check grasshopper can reach target or not
- 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 convert string in K moves or not using Python
- Program to check whether we can split list into consecutive increasing sublists or not in Python

Advertisements