

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Program to check robot can reach target position or not in Python
- Program to check we can reach at position n by jumping or not in Python
- Program to check person can reach top-left or bottomright cell avoiding fire or not in Python
- C++ code to check grasshopper can reach target or not
- 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
- 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 convert string in K moves or not using Python
- C++ program to check we can buy product with given money or not
Advertisements