package com.fishercoder.solutions;

/**766. Toeplitz Matrix
 *
 * A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.

 Now given an M x N matrix, return True if and only if the matrix is Toeplitz.

 Example 1:

 Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]]
 Output: True
 Explanation:
 1234
 5123
 9512

 In the above grid, the diagonals are "[9]", "[5, 5]", "[1, 1, 1]", "[2, 2, 2]", "[3, 3]", "[4]",
 and in each diagonal all elements are the same, so the answer is True.

 Example 2:

 Input: matrix = [[1,2],[2,2]]
 Output: False
 Explanation:
 The diagonal "[1, 2]" has different elements.

 Note:

 matrix will be a 2D array of integers.
 matrix will have a number of rows and columns in range [1, 20].
 matrix[i][j] will be integers in range [0, 99].

 */
public class _766 {
  public static class Solution1 {
    public boolean isToeplitzMatrix(int[][] matrix) {
      int m = matrix.length;
      int n = matrix[0].length;
      int i = 0;
      int j = 0;
      int sameVal = matrix[i][j];
      while (++i < m && ++j < n) {
        if (matrix[i][j] != sameVal) {
          return false;
        }
      }

      for (i = 1, j = 0; i < m; i++) {
        int tmpI = i;
        int tmpJ = j;
        sameVal = matrix[i][j];
        while (++tmpI < m && ++tmpJ < n) {
          if (matrix[tmpI][tmpJ] != sameVal) {
            return false;
          }
        }
      }
      for (i = 0, j = 1; j < n; j++) {
        int tmpJ = j;
        int tmpI = i;
        sameVal = matrix[tmpI][tmpJ];
        while (++tmpI < m && ++tmpJ < n) {
          if (matrix[tmpI][tmpJ] != sameVal) {
            return false;
          }
        }
      }
      return true;
    }
  }
}

@@ -33,778 +33,8 @@
 s;%0A%0A
-/**766. Toeplitz Matrix%0A *%0A * A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same element.%0A%0A Now given an M x N matrix, return True if and only if the matrix is Toeplitz.%0A%0A Example 1:%0A%0A Input: matrix = %5B%5B1,2,3,4%5D,%5B5,1,2,3%5D,%5B9,5,1,2%5D%5D%0A Output: True%0A Explanation:%0A 1234%0A 5123%0A 9512%0A%0A In the above grid, the diagonals are %22%5B9%5D%22, %22%5B5, 5%5D%22, %22%5B1, 1, 1%5D%22, %22%5B2, 2, 2%5D%22, %22%5B3, 3%5D%22, %22%5B4%5D%22,%0A and in each diagonal all elements are the same, so the answer is True.%0A%0A Example 2:%0A%0A Input: matrix = %5B%5B1,2%5D,%5B2,2%5D%5D%0A Output: False%0A Explanation:%0A The diagonal %22%5B1, 2%5D%22 has different elements.%0A%0A Note:%0A%0A matrix will be a 2D array of integers.%0A matrix will have a number of rows and columns in range %5B1, 20%5D.%0A matrix%5Bi%5D%5Bj%5D will be integers in range %5B0, 99%5D.%0A%0A */%0A
 publ
@@ -49,16 +49,18 @@
  _766 %7B%0A
+  
   public
@@ -85,16 +85,20 @@
 tion1 %7B%0A
+    
     publ
@@ -139,24 +139,30 @@
 %5D matrix) %7B%0A
+      
       int m 
@@ -180,24 +180,30 @@
 ngth;%0A      
+  
+    
 int n = matr
@@ -212,24 +212,30 @@
 %5B0%5D.length;%0A
+      
       int i 
@@ -235,24 +235,30 @@
  int i = 0;%0A
+      
       int j 
@@ -258,24 +258,30 @@
  int j = 0;%0A
+      
       int sa
@@ -304,24 +304,30 @@
 %5D%5Bj%5D;%0A      
+  
+    
 while (++i %3C
@@ -343,32 +343,40 @@
  %3C n) %7B%0A        
+    
+    
 if (matrix%5Bi%5D%5Bj%5D
@@ -382,32 +382,42 @@
 %5D != sameVal) %7B%0A
+          
           return
@@ -436,25 +436,45 @@
     
+  
-%7D%0A      %7D%0A%0A
+      %7D%0A            %7D%0A%0A    
       
+  
 for 
@@ -498,24 +498,32 @@
 %3C m; i++) %7B%0A
+        
         int 
@@ -532,32 +532,40 @@
 pI = i;%0A        
+      
+  
 int tmpJ = j;%0A  
@@ -554,32 +554,40 @@
   int tmpJ = j;%0A
+        
         sameVal 
@@ -602,32 +602,40 @@
 %5Bi%5D%5Bj%5D;%0A        
+      
+  
 while (++tmpI %3C 
@@ -655,32 +655,42 @@
  n) %7B%0A          
+      
+    
 if (matrix%5BtmpI%5D
@@ -714,32 +714,44 @@
 ) %7B%0A            
+    
+        
 return false;%0A  
@@ -754,32 +754,60 @@
 ;%0A          
+        
+  %7D%0A                
 %7D%0A      
   %7D%0A      %7D%0A
@@ -790,36 +790,32 @@
     %7D%0A      
-  %7D%0A
       %7D%0A
       for (i
@@ -798,24 +798,30 @@
           %7D%0A
+      
       for (i
@@ -843,24 +843,32 @@
 %3C n; j++) %7B%0A
+        
         int 
@@ -877,32 +877,40 @@
 pJ = j;%0A        
+  
+      
 int tmpI = i;%0A  
@@ -899,32 +899,40 @@
   int tmpI = i;%0A
+        
         sameVal 
@@ -953,32 +953,40 @@
 %5BtmpJ%5D;%0A        
+  
+      
 while (++tmpI %3C 
@@ -996,32 +996,42 @@
 & ++tmpJ %3C n) %7B%0A
+          
           if (ma
@@ -1053,32 +1053,44 @@
 %5D != sameVal) %7B%0A
+            
             retu
@@ -1113,28 +1113,58 @@
     
+    
-%7D%0A        %7D%0A      %7D%0A
+      %7D%0A                %7D%0A            %7D%0A      
     
@@ -1182,16 +1182,22 @@
 ue;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 766