我一直在试图解决黑客排名alternating character problem,但我被卡住了。有谁能给这个问题一个解释或更好的方法吗。谢谢!在
print("Enter the number of test cases: ")
T = int(input())
line = T
while line > 0:
test_string = input()
i = 0
counter = 0
while i < (len(test_string)):
if (test_string[i - 1] == test_string[i] and len(test_string) > 2):
test_string = test_string[i:]
counter += 1
elif (len(test_string) <= 2):
break
i += 1
print (counter)
line -= 1
如果前一个字符与当前字符相同,我们只需要删除一个字符:
或使用sum and range获取所有分组:
^{pr2}$Python版本:通过基本编码
这很简单:)
如果允许使用标准python libs,可以使用
itertools.groupby
进行尝试:这是Linux命令的Python版本
uniq
。在相关问题 更多 >
编程相关推荐