remote87

Are Similar?

Apr 12th, 2021 (edited)
520
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.58 KB | None | 0 0
  1. boolean areSimilar(int[] a, int[] b) {
  2.    
  3.     if(Arrays.equals(a, b)) return true;
  4.    
  5.     int mismatch1 = -1;
  6.     int mismatch2 = -1;
  7.     int temp = 0;
  8.    
  9.     for(int i = 0; i < a.length; i++){
  10.         if(a[i] != b[i]) {
  11.             if(mismatch1 == -1) {
  12.                 mismatch1 = i;
  13.                 continue;
  14.             }
  15.             else{
  16.                 mismatch2 = i;
  17.                 break;
  18.             }
  19.         }
  20.     }
  21.    
  22.     temp = a[mismatch1];
  23.     a[mismatch1] = a[mismatch2];
  24.     a[mismatch2] = temp;
  25.    
  26.     return Arrays.equals(a, b);
  27.    
  28. }
  29.  
Add Comment
Please, Sign In to add comment