LeetCode5458. 字符串的好分割数目

class Solution:
    def numSplits(self, s: str) -> int:
        a = []
        b = []

        sa = set()
        sb = set()

        i = 0
        cnt = 0
        while i < len(s):
            if s[i] not in sa:
                sa.add(s[i])
                cnt += 1
            a.append(cnt)
            i += 1

        i = len(s) - 1
        cnt = 0
        while i >= 0:
            if s[i] not in sb:
                sb.add(s[i])
                cnt += 1
            b.append(cnt)
            i -= 1

        b.reverse()

        cnt = 0
        for i in range(0, len(s) - 1):
            if a[i] == b[i+1]:
                cnt += 1
        return cnt

你可能感兴趣的:(算法)