Advertisement
brewersfan1976

create_anagram.rb

Oct 1st, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.56 KB | None | 0 0
  1. def createAnagram(s, t)
  2.     cnt1 = Array.new(26)
  3.     cnt2 = Array.new(26)
  4.     x = 0
  5.    
  6.     while (x < 26)
  7.           cnt1[x] = 0
  8.           cnt2[x] = 0
  9.           x = x + 1
  10.     end
  11.    
  12.     x = 0
  13.    
  14.     while (x < s.length)
  15.           cnt1[s[x].ord - 'A'.ord] = cnt1[s[x].ord - 'A'.ord] + 1
  16.           cnt2[t[x].ord - 'A'.ord] = cnt2[t[x].ord - 'A'.ord] + 1
  17.           x = x + 1
  18.     end
  19.    
  20.     ans = 0
  21.     x = 0
  22.    
  23.     while (x < 26)
  24.           ans = ans + (cnt1[x] - cnt2[x]).abs
  25.           x = x + 1
  26.     end
  27.    
  28.     return ans / 2
  29.    
  30. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement