summaryrefslogtreecommitdiff
path: root/misc/subsequence/main.rb
blob: 0206793c7e6870d1996b61643e56c7e469bc9d9d (plain)
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
def assert_equal(x, y)
  if x == y
    puts 'PASS!'
  else
    puts "FAIL!: expected `#{x.inspect}` got `#{y.inspect}`"
  end
end

=begin
 x
|a|b|c|d|e|f|

 y
|a|*|d|*|f|
=end

class Solution
  def self.run(sequence, pattern)
    p = 0
    index = nil

    0.upto(sequence.length - 1) do |n|
      x = sequence[n]
      y = pattern[p]

      if y == '*'
        index = n if p.zero?
        l = pattern[p + 1]

        p += 2 if l == x
      elsif x == y
        index = n if p.zero?

        p += 1
      else
        index = nil unless p >= pattern.size
        p = 0
      end
    end

    index
  end

  class Input
    attr_reader :position

    def initialize(string)
      @string = string
      @position = 0
    end

    def seek(position)
      @position = position
    end

    def previous
      @string.chars[position - 1]
    end

    def current
      @string.chars[position]
    end

    def peek
      @string.chars[position + 1]
    end

    def advance(n)
      @position += n
    end

    def eos?
      @position == @string.size
    end
  end

  class Atom
    attr_accessor :next

    def parse(string)
      match?(Input.new(string))
    end

    def match_next?(input)
      self.next.match?(input)
    end

    def to_s
      self.class.to_s
    end

    def inspect
      "#{self}->#{self.next&.inspect}"
    end
  end

  class Start < Atom
    def match?(input)
      result = nil

      until input.eos?
        result = input.position
        break if match_next?(input)

        input.seek(result + 1)
        result = nil
      end

      result
    end

    def to_s
      "start"
    end
  end

  class Wildcard < Atom
    def initialize(lookahead)
      @lookahead = lookahead
    end

    def match?(input)
      return true if input.eos?

      if input.peek == @lookahead
        input.advance(1)
        match_next?(input)
      else
        input.advance(1)
        match?(input)
      end
    end

    def to_s
      "*"
    end
  end

  class Final < Atom
    def match?(*)
      true
    end

    def to_s
      "final"
    end
  end

  class Literal < Atom
    attr_reader :value

    def initialize(value)
      @value = value
    end

    def match?(input)
      if value == input.current
        input.advance(1)
        match_next?(input)
      else
        false
      end
    end

    def to_s
      @value
    end
  end

  def self.build_machine(pattern)
    initial = Start.new
    machine = initial

    for i in 0...pattern.size
      atom = case pattern[i]
      when '*'
        Wildcard.new(pattern[i + 1])
      else
        Literal.new(pattern[i])
      end

      machine.next = atom
      machine = atom
    end
    machine.next = Final.new

    initial
  end

  def self.run(sequence, pattern)
    machine = build_machine(pattern)
    machine.parse(sequence)
  end

  def self.state_for(token)
    return :final if token.nil?

    case token
    when '*'
      :wildcard
    else
      :literal
    end
  end

  def self.debug(state, sequence_pos, pattern_pos, sequence, pattern)
    return unless ENV['DEBUG']

    puts [state].inspect
    puts "  " + (" " * sequence_pos) + "v"
    puts "  " + sequence
    puts "  " + (" " * pattern_pos) + "v"
    puts "  " + pattern
  end

  # start, literal, wildcard, final
  def self.run(sequence, pattern)
    state = :start
    sequence_pos = 0
    pattern_pos = 0
    result = nil

    while pattern_pos < pattern.size && sequence_pos < sequence.size
      debug(state, sequence_pos, pattern_pos, sequence, pattern)

      case state
      when :start
        pattern_pos = 0
        result = sequence_pos
        state = state_for(pattern[pattern_pos])
      when :literal
        if sequence[sequence_pos] == pattern[pattern_pos]
          pattern_pos += 1
          state = state_for(pattern[pattern_pos])
        else
          state = :start
          result = nil
        end

        sequence_pos += 1
      when :wildcard
        lookahead = pattern[pattern_pos + 1]
        if sequence[sequence_pos] == lookahead
          state = state_for(lookahead)
          pattern_pos += 1
        else
          sequence_pos += 1
        end
      when :final
        break
      end
    end

    result
  end

  def self.run(sequence, pattern, index = 0)
    puts [sequence, pattern, index].inspect if ENV['DEBUG']
    return index if pattern.nil? || pattern.empty?
    return index if sequence.nil? || sequence.empty?

    case (atom = pattern[0])
    when '*'
      if sequence[1] == pattern[1]
        run(sequence[1..-1], pattern[1..-1], index)
      else
        run(sequence[1..-1], pattern, index)
      end
    else
      if sequence[0] == atom
        run(sequence[1..-1], pattern[1..-1], index)
      else
        run(sequence[1..-1], pattern, index + 1)
      end
    end
  end

  def self.run(sequence, pattern, index = 0)
    return index if pattern.empty?
    return index if sequence.empty?

    atom = pattern[0]
    if atom != '*'
      if sequence[0] == atom
        run(sequence[1..-1], pattern[1..-1], index)
      else
        run(sequence[1..-1], pattern, index + 1)
      end
    else
      if sequence[1] == pattern[1]
        run(sequence[1..-1], pattern[1..-1], index)
      else
        run(sequence[1..-1], pattern, index)
      end
    end
  end
end
=begin
 x
|a|b|c|d|e|f|

 y
|a|*|d|*|f|

=end

assert_equal 0, Solution.run('abcdef', 'a*d*f')
assert_equal 0, Solution.run('abcdefg', 'a*d*f')
assert_equal 0, Solution.run('abcdefxyz', 'a*d*f')
assert_equal 0, Solution.run('mo is the best', 'm* is the ****')
assert_equal 1, Solution.run('abcd', 'b*d')
assert_equal 1, Solution.run('abcddef', 'bc*de')
assert_equal 2, Solution.run('abacd', 'ac*')
assert_equal 3, Solution.run('xyzabcdef', 'a*d*f')
assert_equal 7, Solution.run('Phone: +1(403)-555-5555', '+*(***)-***-****')