-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_the_longest_balanced_substring_of_a_binary_string.rb
54 lines (50 loc) · 1.34 KB
/
find_the_longest_balanced_substring_of_a_binary_string.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
# https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string/description/
# 2609. Find the Longest Balanced Substring of a Binary String
# @param {String} s
# @return {Integer}
def find_the_longest_balanced_substring(s)
(s.scan(/0+1+/).map { |s| [s.count(?0), s.count(?1)].min }.max || 0) * 2
end
puts "find_the_longest_balanced_substring"
p find_the_longest_balanced_substring("01000111")
# Output: 6
p find_the_longest_balanced_substring("00111")
# Output: 4
p find_the_longest_balanced_substring("111")
# Output: 0
p find_the_longest_balanced_substring("111011")
# Output: 2
def find_the_longest_balanced_substring_ii(s)
out = 0
zeros = 0
ones = 0
s.each_char do |letter|
if letter=="0"
if ones==0
# continue counting
zeros += 1
else
# break counting
ones = 0
zeros = 1
end
elsif letter=="1"
if zeros>0
ones += 1
# compare ones with zeros and add to result
out = [zeros, ones].min
end
end
end
# fix the result
out * 2
end
puts "find_the_longest_balanced_substring_ii"
p find_the_longest_balanced_substring_ii("01000111")
# Output: 6
p find_the_longest_balanced_substring_ii("00111")
# Output: 4
p find_the_longest_balanced_substring_ii("111")
# Output: 0
p find_the_longest_balanced_substring_ii("111011")
# Output: 2