Cho chuỗi astro_dict đại diện thứ tự từ điển một phần của cổ astronaut. Cho chuỗi s, kiểm tra s có tuân theo thứ tự từ điển đó không.
Ví dụ: dictionary = "bdc", s = "bbbb h ddd i cccc" → True.
Các bước giải quyết
l = len(astro_dict), nếul == 0→Truei = 0- Với mỗi ký tự
ctrongs:- Nếu
c in astro_dict:- Khi
i < lvàastro_dict[i] != c:i += 1 - Nếu
i >= lhoặcastro_dict[i] != c→False
- Khi
- Nếu
- Trả về
True
Ví dụ mã Python
class Solution:
def solve(self, astro_dict, s):
l = len(astro_dict)
if l == 0:
return True
i = 0
for c in s:
if c in astro_dict:
while i < l and astro_dict[i] != c:
i += 1
if i >= l or astro_dict[i] != c:
return False
return True
ob = Solution()
print(ob.solve("bdc","bbbb h ddd i cccc"))
Đầu vào
"bdc","bbbb h ddd i cccc"
Đầu ra
True