Skip to content

Commit

Permalink
2025-01-27 v. 8.2.8: added "1079. Letter Tile Possibilities"
Browse files Browse the repository at this point in the history
  • Loading branch information
fartem authored Jan 27, 2025
2 parents 1025319 + 6e38a50 commit bd8af17
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 1026. Maximum Difference Between Node and Ancestor | [Link](https://leetcode.com/problems/maximum-difference-between-node-and-ancestor/) | [Link](./lib/medium/1026_maximum_difference_between_node_and_ancestor.rb) | [Link](./test/medium/test_1026_maximum_difference_between_node_and_ancestor.rb) |
| 1038. Binary Search Tree to Greater Sum Tree | [Link](https://leetcode.com/problems/binary-search-tree-to-greater-sum-tree/) | [Link](./lib/medium/1038_binary_search_tree_to_greater_sum_tree.rb) | [Link](./test/medium/test_1038_binary_search_tree_to_greater_sum_tree.rb) |
| 1043. Partition Array for Maximum Sum | [Link](https://leetcode.com/problems/partition-array-for-maximum-sum/) | [Link](./lib/medium/1043_partition_array_for_maximum_sum.rb) | [Link](./test/medium/test_1043_partition_array_for_maximum_sum.rb) |
| 1079. Letter Tile Possibilities | [Link](https://leetcode.com/problems/letter-tile-possibilities/) | [Link](./lib/medium/1079_letter_tile_possibilities.rb) | [Link](./test/medium/test_1079_letter_tile_possibilities.rb) |
| 1400. Construct K Palindrome Strings | [Link](https://leetcode.com/problems/construct-k-palindrome-strings/) | [Link](./lib/medium/1400_construct_k_palindrome_strings.rb) | [Link](./test/medium/test_1400_construct_k_palindrome_strings.rb) |
| 2116. Check if a Parentheses String Can Be Valid | [Link](https://leetcode.com/problems/check-if-a-parentheses-string-can-be-valid/) | [Link](./lib/medium/2116_check_if_a_parentheses_string_can_be_valid.rb) | [Link](./test/medium/test_2116_check_if_a_parentheses_string_can_be_valid.rb) |
| 2425. Bitwise XOR of All Pairings | [Link](https://leetcode.com/problems/bitwise-xor-of-all-pairings/) | [Link](./lib/medium/2425_bitwise_xor_of_all_pairings.rb) | [Link](./test/medium/test_2425_bitwise_xor_of_all_pairings.rb) |
Expand Down
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '8.2.7'
s.version = '8.2.8'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
s.executable = 'leetcode-ruby'
Expand Down
30 changes: 30 additions & 0 deletions lib/medium/1079_letter_tile_possibilities.rb
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
34 changes: 34 additions & 0 deletions test/medium/test_1079_letter_tile_possibilities.rb
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

0 comments on commit bd8af17

Please sign in to comment.