-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSuperReducedString.rb
108 lines (66 loc) · 2.21 KB
/
SuperReducedString.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
=begin
#---------------------------------------------------------------------------------------------------------
#=========================================================================================================
#
# SUPER REDUCED STRING
#
#=========================================================================================================
#---------------------------------------------------------------------------------------------------------
Steve has a string of lowercase characters in range ascii[‘a’..’z’].
He wants to reduce the string to its shortest length by doing a series of operations.
In each operation he selects a pair of adjacent lowercase letters that match, and he deletes them.
For instance, the string aab could be shortened to b in one operation.
Steve’s task is to delete as many characters as possible using this method and print the resulting string. If the final string is empty, print Empty String
Function Description
Complete the superReducedString function in the editor below. It should return the super reduced string or Empty String if the final string is empty.
superReducedString has the following parameter(s):
s: a string to reduce
Input Format
A single string, .
Constraints
Output Format
If the final string is empty, print Empty String; otherwise, print the final non-reducible string.
Sample Input 0
aaabccddd
Sample Output 0
abd
Explanation 0
Steve performs the following sequence of operations to get the final string:
aaabccddd → abccddd → abddd → abd
Sample Input 1
aa
Sample Output 1
Empty String
Explanation 1
aa → Empty String
Sample Input 2
baab
Sample Output 2
Empty String
Explanation 2
baab → bb → Empty String
=end
input1 = "aaabccddd"
input2 = "aa"
input3 = "baab"
input4 = "mmssnnhhbbmmggxxaaooeeqqeennffzzaaeeyyaaeessvvssggbbccnnrrjjxxuuzzbb"
# Approach 1
class ReducedString
def intialize(input)
@input = input
end
def approach1()
end
end
def approach1(input)
result = nil
while (input =~ /(.)\1/)
input.gsub! /(.)\1/, '' #One or more characters
end
result = input.empty? ? 'Empty String' : input
return result
end
p approach1(input1)
p approach1(input2)
p approach1(input3)
p approach1(input4)