Skip to content

Commit 5e4c784

Browse files
committed
2024-01-17 v. 3.9.8: added "1961. Check If String Is a Prefix of Array"
1 parent 9088e83 commit 5e4c784

File tree

4 files changed

+40
-1
lines changed

4 files changed

+40
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,3 +344,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
344344
| 1941. Check if All Characters Have Equal Number of Occurrences | [Link](https://leetcode.com/problems/check-if-all-characters-have-equal-number-of-occurrences/description/) | [Link](./lib/easy/1941_check_if_all_characters_have_equal_number_of_occurrences.rb) |
345345
| 1952. Three Divisors | [Link](https://leetcode.com/problems/three-divisors/description/) | [Link](./lib/easy/1952_three_divisors.rb) |
346346
| 1957. Delete Characters to Make Fancy String | [Link](https://leetcode.com/problems/delete-characters-to-make-fancy-string/description/) | [Link](./lib/easy/1957_delete_characters_to_make_fancy_string.rb) |
347+
| 1961. Check If String Is a Prefix of Array | [Link](https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/description/) | [Link](./lib/easy/1961_check_if_string_is_a_prefix_of_array.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
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 = '3.9.7'
8+
s.version = '3.9.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/check-if-string-is-a-prefix-of-array/description/
4+
# @param {String} s
5+
# @param {String[]} words
6+
# @return {Boolean}
7+
def is_prefix_string(s, words)
8+
p = 0
9+
i = 0
10+
while i < words.length && p < s.length
11+
word = words[i]
12+
j = p
13+
k = 0
14+
while j < s.length && k < word.length
15+
return false unless s[j] == word[k]
16+
17+
j += 1
18+
k += 1
19+
end
20+
21+
p += word.length
22+
i += 1
23+
end
24+
25+
p == s.length
26+
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/easy/1961_check_if_string_is_a_prefix_of_array'
5+
require 'minitest/autorun'
6+
7+
class CheckIfStringIsAPrefixOfArrayTest < ::Minitest::Test
8+
def test_default
9+
assert(is_prefix_string('iloveleetcode', %w[i love leetcode apples]))
10+
assert(!is_prefix_string('iloveleetcode', %w[apples i love leetcode]))
11+
end
12+
end

0 commit comments

Comments
 (0)