package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * https://leetcode.com/problems/flip-columns-for-maximum-number-of-equal-rows/
 */
public class Problem1072 {
    public int maxEqualRowsAfterFlips(int[][] matrix) {
        Map<String, Integer> map = new HashMap<>();
        for (int i = 0; i < matrix.length; i++) {
            String original = "";
            String flipped = "";
            for (int j = 0; j < matrix[i].length; j++) {
                original += matrix[i][j];
                flipped += matrix[i][j] == 0 ? "1" : "0";
            }
            if (!map.containsKey(original) && !map.containsKey(flipped)) {
                map.put(original, 1);
            } else if (map.containsKey(original)) {
                map.put(original, map.get(original) + 1);
            } else {
                map.put(flipped, map.get(flipped) + 1);
            }
        }
        int answer = 0;
        for (int count : map.values()) {
            answer = Math.max(answer, count);
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1072 prob = new Problem1072();
        System.out.println(prob.maxEqualRowsAfterFlips(new int[][]{
            {0, 1},
            {1, 1}
        })); // 1
        System.out.println(prob.maxEqualRowsAfterFlips(new int[][]{
            {0, 1},
            {1, 0}
        })); // 2
        System.out.println(prob.maxEqualRowsAfterFlips(new int[][]{
            {0, 0, 0},
            {0, 0, 1},
            {1, 1, 0}
        })); // 2
        System.out.println(prob.maxEqualRowsAfterFlips(new int[][]{
            {0, 0, 0},
            {0, 0, 1},
            {1, 0, 1},
            {1, 1, 0}
        })); // 2
    }
}

@@ -228,24 +228,48 @@
 %5D matrix) %7B%0A
+        int answer = 0;%0A
         Map%3C
@@ -354,24 +354,234 @@
 gth; i++) %7B%0A
+            // count all the number of rows that are opposite of each other, e.g.%0A            // 1001%0A            // 1001%0A            // 0110 --%3E if we flip this, it will be 1001%0A            // total count = 3%0A
             
@@ -798,32 +798,59 @@
 ;%0A            %7D%0A
+            int count = 0;%0A
             if (
@@ -904,24 +904,51 @@
 flipped)) %7B%0A
+                count = 1;%0A
             
@@ -969,17 +969,21 @@
 iginal, 
-1
+count
 );%0A     
@@ -1045,33 +1045,23 @@
         
-map.put(original,
+count =
  map.get
@@ -1070,25 +1070,24 @@
 riginal) + 1
-)
 ;%0A          
@@ -1092,531 +1092,145 @@
     
-%7D else %7B%0A                map.put(flipped, map.get(flipped) + 1);%0A            %7D%0A        %7D%0A        int answer = 0;%0A        for (int count : map.values()) %7B%0A            answer = Math.max(answer, count);%0A        %7D%0A        return answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1072 prob = new Problem1072();%0A        System.out.println(prob.maxEqualRowsAfterFlips(new int%5B%5D%5B%5D%7B%0A            %7B0, 1%7D,%0A            %7B1, 1%7D%0A        %7D)); // 1%0A        System.out.println(prob.maxEqualRowsAfterFlips(new int%5B%5D%5B%5D%7B
+    map.put(original, count);%0A            %7D else %7B%0A                count = map.get(flipped) + 1;%0A                map.put(flipped, count);
 %0A   
@@ -1242,15 +1242,9 @@
     
-%7B0, 1%7D,
+%7D
 %0A   
@@ -1256,363 +1256,74 @@
     
-%7B1, 0%7D%0A        %7D)); // 2%0A        System.out.println(prob.maxEqualRowsAfterFlips(new int%5B%5D%5B%5D%7B%0A            %7B0, 0, 0%7D,%0A            %7B0, 0, 1%7D,%0A            %7B1, 1, 0%7D%0A        %7D)); // 2%0A        System.out.println(prob.maxEqualRowsAfterFlips(new int%5B%5D%5B%5D%7B%0A            %7B0, 0, 0%7D,%0A            %7B0, 0, 1%7D,%0A            %7B1, 0, 1%7D,%0A            %7B1, 1, 0%7D%0A        %7D)); // 2
+answer = Math.max(answer, count);%0A        %7D%0A        return answer;
 %0A   

Solve problem 1072