判断字符串中是否存在连续子串 'algorithmalgorithm' - Python 代码示例

要判断一个字符串中是否存在连续子串 'algorithmalgorithm',我们可以通过遍历字符串的每个位置,逐个比较是否与目标子串相同。

以下是一个 Python 编程示例:

def check_substring_exists(N, S):
    target = 'algorithmalgorithm'
    target_len = len(target)
    
    # 遍历字符串的每个位置
    for i in range(N - target_len + 1):
        # 比较当前位置及之后的子串是否与目标子串相同
        if S[i:i+target_len] == target:
            return 'YES'
    
    return 'NO'

# 读取输入
N = int(input())
S = input()

# 判断是否存在连续子串 'algorithmalgorithm'
result = check_substring_exists(N, S)

# 输出结果
print(result)

你可以将以上代码保存为一个 Python 文件,然后运行并输入对应的输入,程序会根据判断结果输出是 'YES' 还是 'NO',表示字符串中是否存在连续子串 'algorithmalgorithm'。

判断字符串中是否存在连续子串 'algorithmalgorithm' - Python 代码示例

原文地址: https://www.cveoy.top/t/topic/bsuI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录