สมมติว่าเรามีสตริงที่มี R (ตำแหน่งปัจจุบัน), B (บล็อก), . (ว่าง) ในหนึ่งก้าวเดินไปตำแหน่งติดกันได้ถ้าว่าง ต้องตรวจสอบว่าถึงขอบซ้ายสุดหรือขวาสุดได้หรือไม่
ตัวอย่าง: s = "...........R.....BBBB....." ผลลัพธ์ True (ไปซ้ายสุดได้ ไม่มี B บлок)
ขั้นตอนการแก้ปัญหา (Simple Check)
r_pos = s.find('R')- คืนค่า
Trueหาก'B' not in s[:r_pos]หรือ'B' not in s[r_pos:]
ตัวอย่างโค้ด Python
class Solution:
def solve(self, s):
r_pos = s.find('R')
return 'B' not in s[:r_pos] or 'B' not in s[r_pos:]
ob = Solution()
s = "...........R.....BBBB....."
print(ob.solve(s)) # Output: True
อินพุต
"...........R.....BBBB....."
เอาต์พุต
True