diff --git a/README.md b/README.md index 55051f9f..af0cb383 100644 --- a/README.md +++ b/README.md @@ -638,6 +638,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 894. All Possible Full Binary Trees | [Link](https://leetcode.com/problems/all-possible-full-binary-trees/) | [Link](./lib/medium/894_all_possible_full_binary_trees.rb) | [Link](./test/medium/test_894_all_possible_full_binary_trees.rb) | | 901. Online Stock Span | [Link](https://leetcode.com/problems/online-stock-span/) | [Link](./lib/medium/901_online_stock_span.rb) | [Link](./test/medium/test_901_online_stock_span.rb) | | 916. Word Subsets | [Link](https://leetcode.com/problems/word-subsets/) | [Link](./lib/medium/916_word_subsets.rb) | [Link](./test/medium/test_916_word_subsets.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) | ### Hard diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 0d0ec44e..fb204ef0 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -5,7 +5,7 @@ require 'English' ::Gem::Specification.new do |s| s.required_ruby_version = '>= 3.0' s.name = 'leetcode-ruby' - s.version = '7.8.9' + s.version = '7.9.0' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1400_construct_k_palindrome_strings.rb b/lib/medium/1400_construct_k_palindrome_strings.rb new file mode 100644 index 00000000..f5121fb9 --- /dev/null +++ b/lib/medium/1400_construct_k_palindrome_strings.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/construct-k-palindrome-strings/ +# @param {String} s +# @param {Integer} k +# @return {Boolean} +def can_construct_k_palindrome(s, k) + return false if s.size < k + + chars = s.chars.sort + odd_count = 0 + i = 0 + + while i < chars.size + current = chars[i] + count = 0 + + while i < chars.size && chars[i] == current + count += 1 + i += 1 + end + + odd_count += 1 if count.odd? + end + + odd_count <= k +end diff --git a/test/medium/test_1400_construct_k_palindrome_strings.rb b/test/medium/test_1400_construct_k_palindrome_strings.rb new file mode 100644 index 00000000..8613257e --- /dev/null +++ b/test/medium/test_1400_construct_k_palindrome_strings.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1400_construct_k_palindrome_strings' +require 'minitest/autorun' + +class ConstructKPalindromeStringsTest < ::Minitest::Test + def test_default_one = assert(can_construct_k_palindrome('annabelle', 2)) + + def test_default_two = assert(!can_construct_k_palindrome('leetcode', 3)) + + def test_default_three = assert(can_construct_k_palindrome('true', 4)) +end