-
Notifications
You must be signed in to change notification settings - Fork 0
/
urban_dictionary.rb
42 lines (33 loc) · 923 Bytes
/
urban_dictionary.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
# Design a data structure that supports the following two operations:
# void addWord(word)
# bool search(word)
# search(word) can search a literal word or a regular expression string
# containing only letters a-z or .. A . means it can represent any one letter.
require 'spec_helper'
class WordDictionary
def initialize
@dict = []
end
def add_word(word)
@dict << word
end
def search(word)
@dict.any? { |w| w.match(/\A#{word}\Z/) }
end
end
describe '' do
it 'Example cases' do
wd = WordDictionary.new
wd.add_word('a')
wd.add_word('at')
wd.add_word('ate')
wd.add_word('ear')
expect(wd.search('a')).to eq(true)
expect(wd.search('a.')).to eq(true)
expect(wd.search('a.e')).to eq(true)
expect(wd.search('b')).to eq(false)
expect(wd.search('e.')).to eq(false)
expect(wd.search('ea.')).to eq(true)
expect(wd.search('ea..')).to eq(false)
end
end