Optimizing the brute force O(n*n) approach
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nWorking around the time
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWorking around the time limit by sorting the array and using the previous result \n\n# Complexity\n- Time complexity:\n- O(N*N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n- O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int countPairs(int[] nums) {\n        // HashMap<Integer,HashMap<Integer,Integer>> map = new HashMap<>();\n        Arrays.sort(nums);\n        int count = 0;\n        // for(int i =0;i<nums.length;i++){\n        //     map.put(nums[i],new HashMap<Integer,Integer>());\n        // }\n        int prev = 0;\n        for(int i =0;i<nums.length;i++){\n           if(i>0 && nums[i] == nums[i-1]){\n            count+= prev -1;\n            prev--;\n            continue;\n           }\n           prev = 0;\n           int temp = 0;\n            for(int j= i+1;j<nums.length;j++){\n                if(j>i+1 && nums[j] == nums[j-1]){\n                    count+=temp;\n                    prev+=temp;\n                }\n                else if(almostEqual(nums[i],nums[j])){\n                    count++;\n                    prev++;\n                    temp = 1;\n                }\n                else{\n                    temp = 0;\n                    }\n            }\n        }\n        return count;\n    }\n    public boolean almostEqual(int a,int b){\n        if(a==b){\n            return true;\n        }\n        StringBuilder s1 = new StringBuilder(""+a);\n        StringBuilder s2 = new StringBuilder(""+b);\n        if(s1.length()<s2.length()){\n            while(s1.length()<s2.length()){\n                s1.insert(0,\'0\');\n            }\n        }\n\n        int[] freq = new int[10];\n        for(int i =0;i<s1.length();i++){\n            freq[s1.charAt(i)%10]++;\n        }\n        for(int i =0;i<s2.length();i++){\n            if(--freq[s2.charAt(i)%10] < 0){\n                return false;\n            }\n        }\n\n\n\n        int count =0;\n        int[] arr =new int[4];\n        for(int i =0;i<s1.length();i++){\n            if(s1.charAt(i)!=s2.charAt(i)){\n                if(count == 4){\n                    return false;\n                }\n                arr[count] = i;\n                count++;\n            }\n        }\n        if(count<=3){\n            return true;\n        }\n        if(count == 4){\n            char c = s1.charAt(arr[0]);\n            for(int i=1;i<4;i++){\n                if(c == s2.charAt(arr[i]) && s1.charAt(arr[i]) == s2.charAt(arr[0])){\n                        // System.out.println(s2);\n                        return true;\n                }\n            }\n        }\n        return false;\n    }\n    // 1023    1023\n    // 2130    2310\n    // 3282 \n    // 2823\n}\n```
['Java']