leetcode-1079 活字印刷
题目描述:
你有一套活字字模 tiles,其中每个字模上都刻有一个字母 tiles[i]。返回你可以印出的非空字母序列的数目
解法一:使用库函数
from itertools import permutations
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
res = []
for i in range(1,len(tiles)+1):
res.extend(list(permutations(tiles,i)))
return len(set(res))
解法二:
from itertools import permutations
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def dfs(tiles,flags):
res = 0
for i in range(26):
if flags[i] == 0:
continue
res += 1
flags[i] -= 1
res += dfs(tiles,flags)
flags[i] +=1
return res
flags = [0]*26
for v in tiles:
flags[ord(v)-ord("A")] += 1
return dfs(tiles,flags)