-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
2025-01-27 v. 8.2.8: added "1079. Letter Tile Possibilities"
- Loading branch information
Showing
4 changed files
with
66 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
# https://leetcode.com/problems/letter-tile-possibilities/ | ||
# @param {String} tiles | ||
# @return {Integer} | ||
def num_tile_possibilities(tiles) | ||
@count = 0 | ||
|
||
chars = tiles.chars.sort | ||
backtrack(chars, ::Array.new(chars.size, false)) | ||
|
||
@count - 1 | ||
end | ||
|
||
private | ||
|
||
# @param {String[]} chars | ||
# @param {Boolean[]} used | ||
# @return {Void} | ||
def backtrack(chars, used) | ||
@count += 1 | ||
|
||
chars.each_with_index do |char, i| | ||
next if used[i] || (i.positive? && char == chars[i - 1] && !used[i - 1]) | ||
|
||
used[i] = true | ||
backtrack(chars, used) | ||
used[i] = false | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative '../test_helper' | ||
require_relative '../../lib/medium/1079_letter_tile_possibilities' | ||
require 'minitest/autorun' | ||
|
||
class LetterTilePossibilitiesTest < ::Minitest::Test | ||
def test_default_one | ||
assert_equal( | ||
8, | ||
num_tile_possibilities( | ||
'AAB' | ||
) | ||
) | ||
end | ||
|
||
def test_default_two | ||
assert_equal( | ||
188, | ||
num_tile_possibilities( | ||
'AAABBC' | ||
) | ||
) | ||
end | ||
|
||
def test_default_three | ||
assert_equal( | ||
1, | ||
num_tile_possibilities( | ||
'V' | ||
) | ||
) | ||
end | ||
end |