[PYTHON] Solving with Ruby AtCoder ABC110 C String Manipulation

Introduction

This theme

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

Ruby
Code length(Byte) 375
Execution time(ms) 514
memory(KB) 22780

Summary

Referenced site

Recommended Posts

Solving with Ruby AtCoder ABC110 C String Manipulation
Solving with Ruby, Perl, Java and Python AtCoder ABC 107 B String Manipulation
Solving with Ruby and Python AtCoder ABC011 C Dynamic programming
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
Solving with Ruby and Python AtCoder ABC057 C Prime Factorization Bit Search
Solved AtCoder ABC 114 C-755 with Python3
Solving with Ruby and Python AtCoder ARC 059 C Least Squares
Solving with Ruby and Python AtCoder ABC178 D Dynamic programming
Solving with Ruby and Python AtCoder ABC151 D Breadth-first search
Solving with Ruby and Python AtCoder ABC153 E Dynamic programming
Solving with Ruby and Python AtCoder ARC067 C Prime Factorization
Solving with Ruby and Python AtCoder ABC138 D Adjacency list
Solving with Ruby, Python and numpy AtCoder ABC054 B Matrix operation
Solving with Ruby, Python and networkx AtCoder ABC168 D Adjacency list
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solving with Ruby, Perl, Java and Python AtCoder ABC 165 D Floor function
Solving with Ruby and Python AtCoder Tenka1 Programmer Contest C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder ABC 131 D Array Sorting
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Challenge AtCoder (ABC) 164 with Python! A ~ C problem
Solving with Ruby and Python AtCoder CODE FESTIVAL 2016 qual C B Priority queue
Solve AtCoder ABC166 with python
ABC163 C problem with python3
ABC188 C problem with python3
Solve AtCoder ABC 186 with Python
ABC187 C problem with python
[AtCoder explanation] Control ABC180 A, B, C problems with Python!
Solve with Ruby and Python AtCoder ABC133 D Cumulative sum
Solving with Ruby and Python AtCoder AISING2020 D Iterative Squares
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 A
[AtCoder explanation] Control ABC158 A, B, C problems with Python!
Solving with Ruby, Perl, Java and Python AtCoder ATC 002 B
[AtCoder explanation] Control ABC164 A, B, C problems with Python!
[AtCoder explanation] Control ABC168 A, B, C problems with Python!
Solve ABC163 A ~ C with Python
Atcoder ABC125 C --GCD on Blackboard
Solve ABC168 A ~ C with Python
Solve ABC167 A ~ C with Python
Solve ABC158 A ~ C with Python
[AtCoder commentary] Win the ABC165 C problem "Many Requirements" with Python!
Solving in Ruby, Python and Java AtCoder ABC141 D Priority Queuing
[AtCoder explanation] Control the A, B, C problems of ABC182 with Python!
Atcoder ABC099 C --Strange Bank Separate Solution
[AtCoder explanation] Control the A, B, C problems of ABC186 with Python!
Solving with Ruby, Perl, Java, and Python AtCoder AGC 033 A Breadth-first search
[AtCoder explanation] Control the A, B, C problems of ABC185 with Python!
[AtCoder] Solve ABC1 ~ 100 A problem with Python
AtCoder ABC176
AtCoder ABC172 C Cumulative Sum Binary Search Solved by Ruby and Python
[AtCoder explanation] Control the A, B, C problems of ABC187 with Python!
AtCoder ABC177
[AtCoder explanation] Control the A, B, C problems of ABC184 with Python!
[AtCoder explanation] Control the A, B, (C), D problems of ABC165 with Python!
[AtCoder explanation] Control the A, B, C, D problems of ABC183 with Python!
Solve with Ruby and Python AtCoder ABC084 D Cumulative sum of prime numbers
Solving in Ruby, Perl, Java, and Python AtCoder ARC 066 C Iterative Squares Hash
[AtCoder explanation] Control the A, B, C, D problems of ABC181 with Python!
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
AtCoder ABC 098 C --Attention Thinking about the answer
AtCoder ABC 174 Python