给你一个字符串 word
,该字符串由数字和小写英文字母组成。
请你用空格替换每个不是数字的字符。例如,"a123bc34d8ef34"
将会变成 " 123 34 8 34"
。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):"123"
、"34"
、"8"
和 "34"
。
返回对 word
完成替换后形成的 不同 整数的数目。
只有当两个整数的 不含前导零 的十进制表示不同, 才认为这两个整数也不同。
1805. 字符串中不同整数的数目 – 力扣(Leetcode)

思路:
先将字母分割了,然后将字符转成数字统计个数就行了。
python3实现:
class Solution:
def numDifferentIntegers(self, word: str) -> int:
# 先将所有的字母变成空格,然后字符串转成数字然后计算个数就行。
new_word = ""
for elem in word:
if str.isdigit(elem):
new_word += elem
else:
new_word += " "
new_word = new_word.split()
for idx in range(len(new_word)):
new_word[idx] = int(new_word[idx])
dic_new_word = collections.Counter(new_word)
return len(dic_new_word)
