Python 中的古代宇航员理论
假设我们有一个字符串词典,该词典表示古代宇航员词典的部分词典序。因此,如果我们有一个字符串 s,我们必须检查它是否根据这个古代宇航员词典是词典序排序的字符串。
因此,如果输入类似于词典 = “bdc”,s = “bbbb h ddd i cccc”,那么输出将为 True
为了解决这个问题,我们将遵循以下步骤 -
l := astro_dict 的大小
如果 l 与 0 相同,则
返回 True
i := 0
对于 s 中的每个字符 c,执行
如果 c 在 astro_dict 中,则
当 i < l 且 astro_dict[i] 不为 c 时,执行
i := i + 1
如果 i >= l 或 astro_dict[i] 不为 c,则
返回 False
返回 True
让我们看看以下实现以获得更好的理解 -
示例
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"))输入
"bdc","bbbb h ddd i cccc"
输出
True
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP