Python中的古代宇航员理论
假设有一个字符串字典,该字典表示古代宇航员字典的部分词典顺序。因此,如果我们有字符串s,则必须根据此古代宇航员词典检查它是否为按字典顺序排序的字符串。
因此,如果输入像字典=“bdc”,s=“bbbbhdddicccc”,则输出为True
为了解决这个问题,我们将遵循以下步骤-
l:=astro_dict的大小
如果l等于0,则
返回True
i:=0
对于s中的每个字符c,
当我<l并且astro_dict[i]不是c时,
如果i>=l或astro_dict[i]不是c,则
我:=我+1
返回False
如果c在astro_dict中,则
返回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
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短