我有一个接受两个字符串的函数,我试图检查一个字符串是否可以被另一个字符串整除,一个字符串可以被扩展任意次数以匹配另一个字符串的长度,如果出现这种情况,我需要返回一个字符串可以被另一个字符串整除所需的最小连接数:
这是我的尝试:
def findSmallestDivisor(s, t):
s_count = len(s)
t_count = len(t)
if s_count % t_count ==0:
return t_count
假设s=bcdbcdbcdbcd
和t=bcdbcd
,那么它需要从t
得到唯一字符串bcd
的两个浓缩,以被s
整除
我想返回唯一集的长度,以防它是可除的,在上面的例子中是3
根据要求添加更多示例:
假设字符串s=lrbblrbb
和字符串t=lrbb
,那么如果我将字符串t
加倍,它将被字符串s
整除,因为字符串t
有长度4
,这应该是答案,但是在我上面提到的示例中,棘手的部分是当s=bcdbcdbcdbcd
和t=bcdbcd
。答案应该是3
,而不是6
,因为bcd
是唯一集,而t
中的另一个bcd
只是重复
通过简单的模式检测和字符串扩展,试试这个
代码
输出
相关问题 更多 >
编程相关推荐