brewersfan1976

are_similar.rb

Oct 11th, 2016
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.56 KB | None | 0 0
  1. def areSimilar(a, b)
  2.     if a == b then
  3.        return true
  4.     end
  5.        
  6.     array = Array.new
  7.     i = 0
  8.        
  9.     while (i < a.length)
  10.           if a[i] != b[i] then
  11.              array.push(i)
  12.           end
  13.              
  14.           i = i + 1
  15.     end
  16.        
  17.     if array.length == 0 then
  18.        return true
  19.     end
  20.    
  21.     if array.length != 2 then
  22.        return false
  23.     end
  24.        
  25.     id1 = array[0]
  26.     id2 = array[1]
  27.    
  28.     if a[id1] == b[id2] and a[id2] == b[id1] then
  29.        return true
  30.     end
  31.        
  32.     return false
  33. end
Advertisement
Add Comment
Please, Sign In to add comment