From c31f5958aeff676673ddc2f77372bc46d485eeb2 Mon Sep 17 00:00:00 2001 From: Remy Wang Date: Sat, 2 Dec 2023 21:47:45 +0100 Subject: [PATCH] 1160. Find Words That Can Be Formed by Characters --- README.md | 1 + ...-words-that-can-be-formed-by-characters.rb | 70 +++++++++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 algorithms/ruby/1160-find-words-that-can-be-formed-by-characters.rb diff --git a/README.md b/README.md index 5ddf51e..d74a37a 100644 --- a/README.md +++ b/README.md @@ -317,6 +317,7 @@ | 1143 | Longest Common Subsequence | [Ruby](./algorithms/ruby/1143-longest-common-subsequence.rb) | Medium | | 1146 | Snapshot Array | [Ruby](./algorithms/ruby/1146-snapshot-array.rb) | Medium | | 1155 | Number of Dice Rolls With Target Sum | [Ruby](./algorithms/ruby/1155-number-of-dice-rolls-with-target-sum.rb) | Medium | +| 1160 | Find Words That Can Be Formed by Characters | [Ruby](./algorithms/ruby/1160-find-words-that-can-be-formed-by-characters.rb) | Easy | | 1161 | Maximum Level Sum of a Binary Tree | [Ruby](./algorithms/ruby/1161-maximum-level-sum-of-a-binary-tree.rb) | Medium | | 1162 | As Far from Land as Possible | [Ruby](./algorithms/ruby/1162-as-far-from-land-as-possible.rb) | Medium | | 1187 | Make Array Strictly Increasing | [Ruby](./algorithms/ruby/1187-make-array-strictly-increasing.rb) | Hard | diff --git a/algorithms/ruby/1160-find-words-that-can-be-formed-by-characters.rb b/algorithms/ruby/1160-find-words-that-can-be-formed-by-characters.rb new file mode 100644 index 0000000..15333d8 --- /dev/null +++ b/algorithms/ruby/1160-find-words-that-can-be-formed-by-characters.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +# 1160. Find Words That Can Be Formed by Characters +# Easy +# https://leetcode.com/problems/find-words-that-can-be-formed-by-characters + +=begin +You are given an array of strings words and a string chars. +A string is good if it can be formed by characters from chars (each character can only be used once). +Return the sum of lengths of all good strings in words. + +Example 1: +Input: words = ["cat","bt","hat","tree"], chars = "atach" +Output: 6 +Explanation: The strings that can be formed are "cat" and "hat" so the answer is 3 + 3 = 6. + +Example 2: +Input: words = ["hello","world","leetcode"], chars = "welldonehoneyr" +Output: 10 +Explanation: The strings that can be formed are "hello" and "world" so the answer is 5 + 5 = 10. + +Constraints: +1 <= words.length <= 1000 +1 <= words[i].length, chars.length <= 100 +words[i] and chars consist of lowercase English letters. +=end + +# @param {String[]} words +# @param {String} chars +# @return {Integer} +def count_characters(words, chars) + char_hash = {} + chars.each_char do |char| + char_hash[char] ||= 0 + char_hash[char] += 1 + end + + answer = 0 + words.each do |word| + char_hash_clone = char_hash.clone + + has_all = true + word.each_char do |c| + if char_hash_clone[c] && (char_hash_clone[c] > 0) + char_hash_clone[c] -= 1 + else + has_all = false + break + end + end + + if has_all + answer += word.size + end + end + + answer +end + +# **************** # +# TEST # +# **************** # + +require "test/unit" +class Test_count_characters < Test::Unit::TestCase + def test_ + assert_equal 6, count_characters(["cat","bt","hat","tree"], "atach") + assert_equal 10, count_characters(["hello","world","leetcode"], "welldonehoneyr") + end +end