If you strip the characters from the beginning, then you are left with a shorter string and can subtract its length from the original, giving you the number of characters removed.
return len(s) - len(s.lstrip(target))
Note: Your shown code will immediately return 0 if the first character does not match target
. If you want to check if there is any repeated first character, you don't need to have target
and can just use s[0]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…