You have n tiles, where each tile has one letter tiles[i] printed on it.
Return the number of possible non-empty sequences of letters you can make using the letters printed on those tiles.
class Solution:
def numTilePossibilities(self, tiles: str) -> int:
def backtrack(path, tiles):
seqlist.add(path)
if not tiles:
return path
for i in range(len(tiles)):
backtrack(path + tiles[i], tiles[:i]+tiles[i+1:])
seqlist = set()
backtrack("", tiles)
return len(seqlist)-1
전에 backtrack 으로 조합찾던게 생각나서 그걸로 풀었읍니다
근데 좀 엉성..
You have n gardens, labeled from 1 to n, and an array paths where paths[i] = [xi, yi] describes a bidirectional path between garden xi to garden yi. In each garden, you want to plant one of 4 types of flowers.
All gardens have at most 3 paths coming into or leaving it.
Your task is to choose a flower type for each garden such that, for any two gardens connected by a path, they have different types of flowers.
Return any such a choice as an array answer, where answer[i] is the type of flower planted in the (i+1)th garden. The flower types are denoted 1, 2, 3, or 4. It is guaranteed an answer exists.
class Solution:
def gardenNoAdj(self, n: int, paths: List[List[int]]) -> List[int]:
result = []
pathmap = {}
for i in range(len(paths)):
if paths[i][0] not in pathmap:
pathmap[paths[i][0]] = [paths[i][1]]
else:
pathmap[paths[i][0]].append(paths[i][1])
if n == 0:
return []
start = paths[0][0]
result.append(start)
for i in range(n-1):
if result[-1] in pathmap and pathmap[result[-1]]:
result.append(pathmap[result[-1]][0])
else:
result.append(start)
return result
문제 이해를 잘못해서 망~
이거 뭔가 풀 수 있을 거 같은데.. 안되네요