Skip to content

Commit 8d26297

Browse files
committed
2025-01-11 v. 7.9.0: added "1400. Construct K Palindrome Strings"
1 parent f390da4 commit 8d26297

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
638638
| 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) |
639639
| 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) |
640640
| 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) |
641+
| 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) |
641642

642643
### Hard
643644

leetcode-ruby.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '7.8.9'
8+
s.version = '7.9.0'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[README.md]
1111
s.executable = 'leetcode-ruby'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/construct-k-palindrome-strings/
4+
# @param {String} s
5+
# @param {Integer} k
6+
# @return {Boolean}
7+
def can_construct_k_palindrome(s, k)
8+
return false if s.size < k
9+
10+
chars = s.chars.sort
11+
odd_count = 0
12+
i = 0
13+
14+
while i < chars.size
15+
current = chars[i]
16+
count = 0
17+
18+
while i < chars.size && chars[i] == current
19+
count += 1
20+
i += 1
21+
end
22+
23+
odd_count += 1 if count.odd?
24+
end
25+
26+
odd_count <= k
27+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/1400_construct_k_palindrome_strings'
5+
require 'minitest/autorun'
6+
7+
class ConstructKPalindromeStringsTest < ::Minitest::Test
8+
def test_default_one = assert(can_construct_k_palindrome('annabelle', 2))
9+
10+
def test_default_two = assert(!can_construct_k_palindrome('leetcode', 3))
11+
12+
def test_default_three = assert(can_construct_k_palindrome('true', 4))
13+
end

0 commit comments

Comments
 (0)