From c7cf2c4ccc51e4fb909d06d8c3338710eed34262 Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 17 Feb 2025 08:12:45 +0300 Subject: [PATCH] 2025-02-17 v. 8.5.9: added "1433. Check If a String Can Break Another String" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- ...ck_if_a_string_can_break_another_string.rb | 26 +++++++++++++++++++ ...ck_if_a_string_can_break_another_string.rb | 13 ++++++++++ 4 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 lib/medium/1433_check_if_a_string_can_break_another_string.rb create mode 100644 test/medium/test_1433_check_if_a_string_can_break_another_string.rb diff --git a/README.md b/README.md index 2cfdd96e..b78a900b 100644 --- a/README.md +++ b/README.md @@ -692,6 +692,7 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/). | 1395. Count Number of Teams | [Link](https://leetcode.com/problems/count-number-of-teams/) | [Link](./lib/medium/1395_count_number_of_teams.rb) | [Link](./test/medium/test_1395_count_number_of_teams.rb) | | 1396. Design Underground System | [Link](https://leetcode.com/problems/design-underground-system/) | [Link](./lib/medium/1396_design_underground_system.rb) | [Link](./test/medium/test_1396_design_underground_system.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) | +| 1433. Check If a String Can Break Another String | [Link](https://leetcode.com/problems/check-if-a-string-can-break-another-string/) | [Link](./lib/medium/1433_check_if_a_string_can_break_another_string.rb) | [Link](./test/medium/test_1433_check_if_a_string_can_break_another_string.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) | | 2429. Minimize XOR | [Link](https://leetcode.com/problems/minimize-xor/) | [Link](./lib/medium/2429_minimize_xor.rb) | [Link](./test/medium/test_2429_minimize_xor.rb) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 40dae9f3..6367698a 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 = '8.5.8' + s.version = '8.5.9' s.license = 'MIT' s.files = ::Dir['lib/**/*.rb'] + %w[README.md] s.executable = 'leetcode-ruby' diff --git a/lib/medium/1433_check_if_a_string_can_break_another_string.rb b/lib/medium/1433_check_if_a_string_can_break_another_string.rb new file mode 100644 index 00000000..28503878 --- /dev/null +++ b/lib/medium/1433_check_if_a_string_can_break_another_string.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# https://leetcode.com/problems/check-if-a-string-can-break-another-string/ +# @param {String} s1 +# @param {String} s2 +# @return {Boolean} +def check_if_can_break(s1, s2) + c1 = s1.chars.to_a.sort + c2 = s2.chars.to_a.sort + + is_asc = true + is_desc = true + + (0...c1.size).each do |i| + f = c1[i] + s = c2[i] + + if f < s + is_desc = false + elsif f > s + is_asc = false + end + end + + is_asc || is_desc +end diff --git a/test/medium/test_1433_check_if_a_string_can_break_another_string.rb b/test/medium/test_1433_check_if_a_string_can_break_another_string.rb new file mode 100644 index 00000000..769655d0 --- /dev/null +++ b/test/medium/test_1433_check_if_a_string_can_break_another_string.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +require_relative '../test_helper' +require_relative '../../lib/medium/1433_check_if_a_string_can_break_another_string' +require 'minitest/autorun' + +class CheckIfAStringCanBreakAnotherStringTest < ::Minitest::Test + def test_default_one = assert(check_if_can_break('abc', 'xya')) + + def test_default_two = assert(!check_if_can_break('abe', 'acd')) + + def test_default_three = assert(check_if_can_break('leetcodee', 'interview')) +end