-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path044.rb
More file actions
30 lines (27 loc) · 735 Bytes
/
044.rb
File metadata and controls
30 lines (27 loc) · 735 Bytes
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
# Time Limit Exceeded, correct solution see 044.js
# @param {String} s
# @param {String} p
# @return {Boolean}
def is_match(s, p)
return true if p.length == 0 && s.length == 0
return false if p.length == 0
if s.length == 0
if p[0] == "*"
return is_match("", p[1..p.length-1])
else
return false
end
end
p.sub!('**', '*')
if p[0] == "*"
return true if p.length == 1
next_p = p[1..p.length-1]
return is_match("", next_p) if s == ""
return 0.upto(s.length).any? {|i| is_match(s[i..s.length-1], next_p)}
elsif p[0] == "?"
return is_match(s[1..s.length-1], p[1..p.length-1])
else
return false if s[0] != p[0]
return is_match(s[1..s.length-1], p[1..p.length-1])
end
end