AtCoder Beginner Contest C - String Transformation Difficulty: 946
This theme, string manipulation
Since it is a different lowercase letter, if you think about it the other way around, it means that duplication is taken into consideration, so the duplicate character string is ** hash **. Examine the reverse of the hash so that the back of the coin is the front. Ruby
ruby.rb
s = gets.chomp.chars
t = gets.chomp.chars
h = {}
g = {}
s.size.times do |i|
if h[s[i]]
if h[s[i]].count(t[i]) == 0
h[s[i]] << t[i]
end
else
h[s[i]] = [t[i]]
end
if g[t[i]]
if g[t[i]].count(s[i]) == 0
g[t[i]] << s[i]
end
else
g[t[i]] = [s[i]]
end
end
puts h.flatten(-1) == g.invert.flatten(-1) ? 'Yes' : 'No'
hash.rb
h = {}
g = {}
s.size.times do |i|
if h[s[i]]
if h[s[i]].count(t[i]) == 0
h[s[i]] << t[i]
end
else
h[s[i]] = [t[i]]
end
if g[t[i]]
if g[t[i]].count(s[i]) == 0
g[t[i]] << s[i]
end
else
g[t[i]] = [s[i]]
end
end
We are getting the hash h
that looks at the string t
from the string s
and the hash g
that looks at the string s
from the string t
.
flat.rb
puts h.flatten(-1) == g.invert.flatten(-1) ? 'Yes' : 'No'
Get the reverse of the hash (swap key and value) with ʻinvert` and compare. Python
As you can see in Swap the key and value of the dictionary in one line -Qiita *, it seems difficult to find the reverse of the dictionary, so *** Python *** will be closed. ~~ It doesn't seem to be related to machine learning ~~
When I studied Ruby Engineer Certification Exam Silver *, I thought that I wouldn't use ʻinvert or
flatten`, but in reality I was surprised to use it for.
Basic learning is necessary.
Ruby | |
---|---|
Code length(Byte) | 375 |
Execution time(ms) | 514 |
memory(KB) | 22780 |
Referenced site
Recommended Posts