Java O(n) clear explanation with comments. Beginner friendly
Please upvote it if you like it\nTime complexity - O(n)\nSpace complexity - O(1)\nThe solution has a very simple and clear logic:\nWe search for mismatches in s
**Please upvote it if you like it**\nTime complexity - O(n)\nSpace complexity - O(1)\nThe solution has a very simple and clear logic:\nWe search for mismatches in strings and return true if we have *only two* mismatches **and** when we replace characters by the indexes of these mismatches, the strings will be equal\n\nIsn\'t it clear? \n**Read the code with comments and ask questions, I will be happy to answer them**\n\n```\npublic boolean areAlmostEqual(String s1, String s2) {\n        /*\n        At first, we need to check base cases:\n        If length of strings isn\'t equal, we return false, because\n        No swap will make the strings equal\n         */\n        if (s1.length() != s2.length()) {\n            return false;\n        }\n        /*\n        Here we just check if our string are equal, if they\'re equal, we return false because we can\n        do one swap and string will be still equal\n         */\n        if (s1.equals(s2)) {\n            return true;\n        }\n        /*\n        Let\'s write down the mismatch indexes.\n        For the first string - let\'s call it missmatchAIndex and for the second string - missmatchBIndex\n        and initialize them by -1 because we don\'t know yet where our mismatches\n         */\n        int missmatchAIndex = -1;\n        int missmatchBIndex = -1;\n\n        /*\n        Since the lengths of our strings are equal,\n        we will iterate over each character of the first and second string, but to do this we only need one loop\n         */\n        for (int i = 0; i < s1.length(); i++) {\n            char charS1 = s1.charAt(i); // get the i character of the first string\n            char charS2 = s2.charAt(i); // get the i character of the second string\n            /*\n            If out characters aren\'t equal, then we have found mismatch\n             */\n            if (charS1 != charS2) {\n                /*\n                Since we can only afford two mismatches, we check whether the first (A) mismatch has already occurred.\n                If missmatchAIndex = -1, then there was no first mismatch, and we write the index\n                of the first mismatch to the variable missmatchAIndex.\n                */\n                if (missmatchAIndex == -1) {\n                    missmatchAIndex = i;\n                } else if (missmatchBIndex == -1) {\n                    /*\n                    This is where we get if missmatchAIndex isn\'t equal to -1, so there has already been one mismatch.\n                    We check if missmatchBIndex isn\'t equal to -1. If it is not, then the second mismatch hasn\'t yet\n                    occurred, and we write the second mismatch index to the variable missmatchBIndex.\n                     */\n                    missmatchBIndex = i;\n                } else {\n                    /*\n                    If missmatchAIndex and missmatchBIndex are no longer equal to -1,\n                    and we get here, we immediately return false, because a third mismatch is detected, and we need exactly two.\n                     */\n                    return false;\n                }\n            }\n        }\n\n        /*\n        After going through all the elements in the string,\n        we check if missmatchBIndex = -1, so there was only one mismatch, and we need two, so we return false\n         */\n        if (missmatchBIndex == -1) return false;\n\n        /*\n        If we have exactly two mismatches, we have gone as far as this section of code.\n        Now we need to check that when we replace the mismatched characters, our strings will be the same.\n        This means that the character of the first string available by the first mismatch index (missmatchAIndex) must be equal\n        to the character of the second string available by the second mismatch index (missmatchBIndex) and the character of the first\n        string available by the second mismatch index must be equal to the character of the second string available by the first mismatch index.\n        If this condition is true, the program will return true, otherwise false\n         */\n        return s1.charAt(missmatchAIndex) == s2.charAt(missmatchBIndex) && s1.charAt(missmatchBIndex) == s2.charAt(missmatchAIndex);\n    }\n```\n
['Java']