Easiest Solution explained in detail | C++ | For loops only | KKSolutions
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\nThe maxLengthBetweenE
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe `maxLengthBetweenEqualCharacters` function takes a string `s` as input and aims to find the length of the longest substring between two equal characters.\n\n1. **Initialization:**\n   - We start by initializing a variable named `count` to -1. This variable will be used to store the length of the longest substring.\n\n   ```cpp\n   int count = -1;\n   ```\n\n2. **String Length:**\n   - Calculate the length of the input string `s` and store it in the variable `n`.\n\n   ```cpp\n   int n = s.size();\n   ```\n\n3. **Nested Loops:**\n   - We use two nested loops. The outer loop (`i`) iterates over each character in the string, and the inner loop (`j`) iterates over characters to the right of the current character in the outer loop.\n\n   ```cpp\n   for(int i = 0; i < n; i++) {\n       for(int j = i + 1; j < n; j++) {\n   ```\n\n4. **Character Comparison:**\n   - Check if the characters at positions `i` and `j` are equal.\n\n   ```cpp\n   if(s[i] == s[j]) {\n   ```\n\n5. **Update `count`:**\n   - If equal characters are found, update the `count` variable with the length of the substring between `i` and `j` (excluding the characters themselves). The expression `j - i - 1` represents the distance between the two equal characters minus one.\n\n   ```cpp\n   count = max(count, j - i - 1);\n   ```\n\n6. **Return Result:**\n   - After both loops complete, return the calculated length of the longest substring between equal characters stored in the `count` variable.\n\n   ```cpp\n   return count;\n   ```\n\nIn summary, this function explores all pairs of characters in the string and keeps track of the maximum distance (length) between equal characters. The result is then returned.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n^2)\n0 MS\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n6.66 MB\n# Code\n```\nclass Solution {\npublic:\n    int maxLengthBetweenEqualCharacters(string s) {\n        int count = -1;\n        int n = s.size() ;\n        for(int i= 0; i < n; i++)\n        {\n            for(int j = i+1; j < n; j++)\n            {\n                if(s[i] == s[j])\n                {\n                    count = max(count, j-i-1);\n                }\n            }\n        }\n        return count ;\n    }\n};\n```
['String', 'C++']