-
Notifications
You must be signed in to change notification settings - Fork 3
/
2696-minimum-string-length-after-removing-substrings.rb
62 lines (51 loc) · 1.6 KB
/
2696-minimum-string-length-after-removing-substrings.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# frozen_string_literal: true
# 2696. Minimum String Length After Removing Substrings
# https://leetcode.com/problems/minimum-string-length-after-removing-substrings
# Easy
=begin
You are given a string s consisting only of uppercase English letters.
You can apply some operations to this string where, in one operation, you can remove any occurrence of one of the substrings "AB" or "CD" from s.
Return the minimum possible length of the resulting string that you can obtain.
Note that the string concatenates after removing the substring and could produce new "AB" or "CD" substrings.
Example 1:
Input: s = "ABFCACDB"
Output: 2
Explanation: We can do the following operations:
- Remove the substring "ABFCACDB", so s = "FCACDB".
- Remove the substring "FCACDB", so s = "FCAB".
- Remove the substring "FCAB", so s = "FC".
So the resulting length of the string is 2.
It can be shown that it is the minimum length that we can obtain.
Example 2:
Input: s = "ACBBD"
Output: 5
Explanation: We cannot do any operations on the string so the length remains the same.
Constraints:
1 <= s.length <= 100
s consists only of uppercase English letters.
=end
# @param {String} s
# @return {Integer}
def min_length(s)
arr = []
s.chars.each do |c|
if c == "B" && arr.last == "A"
arr.pop
elsif c == "D" && arr.last == "C"
arr.pop
else
arr.push(c)
end
end
arr.size
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_min_length < Test::Unit::TestCase
def test_
assert_equal 2, min_length("ABFCACDB")
assert_equal 5, min_length("ACBBD")
end
end