[LeetCode] 1079. Letter Tile Possibilities

Updated:

1079. Letter Tile Possibilities

Tile

์œ„ ์˜ˆ์‹œ์ฒ˜๋Ÿผ AAB ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค๋ฉด ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ชจ๋“  ๊ฒฝ์šฐ๋ฅผ ๋”ฐ์ง€๋ฉด ๋œ๋‹ค.

์กฐํ•ฉ์€ AB, BA ๋ฅผ ๊ฐ™์€ ๊ฒฝ์šฐ๋กœ ๋ณด๊ณ  ์ˆœ์—ด์€ AB, BA ๋ฅผ ๋‹ค๋ฅธ ๊ฒฝ์šฐ๋กœ ๋ณธ๋‹ค.

์šฐ์„  ์กฐํ•ฉ์œผ๋กœ ๊ฒฝ์šฐ๋ฅผ ๋”ฐ์ง„ ๋’ค ๊ทธ ๊ฒฝ์šฐ๋งˆ๋‹ค ์ˆœ์—ด์„ ๋งŒ๋“ค๋ฉด ๋œ๋‹ค.

[A, B, AA, AB, AAB] ์— ๋Œ€ํ•ด ๊ฐ๊ฐ ์ˆœ์—ด์„ ๊ตฌํ•˜๋ฉด ๋‹ต์„ ๊ตฌํ•  ์ˆ˜ ์žˆ๋‹ค.

A โ†’ย A

B โ†’ B

AA โ†’ AA

AB โ†’ AB, BA

AAB โ†’ AAB, ABA, BAA

ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ž์—ด์„ ๋”ฐ์ง€๋Š” ๊ฒƒ ์ด๊ธฐ ๋•Œ๋ฌธ์— index 0 ์— ์žˆ๋Š” A ์™€ index 1 ์— ์žˆ๋Š” A ๋ฅผ ๊ฐ™๊ฒŒ ๋ด์•ผํ•œ๋‹ค.

8 ๋ฒˆ์งธ ์ค„์ด๋‚˜ 10 ๋ฒˆ์งธ ์ค„์„ set ์œผ๋กœ ๋ฐ›์•„ ์ฒ˜์Œ๋ถ€ํ„ฐ ์ค‘๋ณต์„ ์ง€์›Œ๋„ ๋˜๊ธด ํ•˜์ง€๋งŒ ๊ทธ๋Ÿฌ๊ธฐ๋ณด๋‹ค

๋งˆ์ง€๋ง‰ ์—ฐ์‚ฐ ํ›„ answer ๋ฅผ set ์œผ๋กœ ๋ฐ”๊ฟ” ์ค‘๋ณต์„ ์ง€์šฐ๋ฉด ์กฐ๊ธˆ์ด๋ผ๋„ ๋น ๋ฅด๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.


import itertools


class Solution:
	def numTilePossibilities(self, tiles: str) -> int:
		answer = []
		for i in range(1, len(tiles) + 1):
			_set = itertools.combinations(tiles, i)
			for j in _set:
				answer += itertools.permutations(j)

		return len(set(answer))


test = Solution()
tiles = 'JDNSJEK'
print(test.numTilePossibilities(tiles))

Categories:

Updated:

Leave a comment