-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathDuplicateEncoder.rb
44 lines (33 loc) · 981 Bytes
/
DuplicateEncoder.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
=begin
The goal of this exercise is to convert a string to a new string where each
character in the new string is '(' if that character appears only once in
the original string, or ')' if that character appears more than once in the
original string. Ignore capitalization when determining if a character
is a duplicate.
Examples:
"din" => "((("
"recede" => "()()()"
"Success" => ")())())"
"(( @" => "))(("
=end
# My Solution
def duplicate_encode(word)
result = ""
new_hash = Hash.new {|d,k| d[k] = 0}
letters = word.split("")
letters.each {|x| new_hash[x.downcase] += 1}
letters.each {|x| result += new_hash[x.downcase] == 1 ? "(" : ")"}
result
end
# Better Solution
def duplicate_encode(word)
word
.downcase
.chars
.map { |char| word.downcase.count(char) > 1 ? letter = ')' : letter = '(' }
.join
end
# Another Solution
def duplicate_encode(word)
word.downcase.chars.map {|x| word.downcase.count(x) > 1 ? ")" : "("}.join("")
end