💥 Kadane's Algorithm Made Simple! 🚀
Power of a String: Finding the Longest Substring with One Unique Character \uD83D\uDD20\uD83D\uDCAA\n\nTo determine the "power" of a string, which is the maximu
### Power of a String: Finding the Longest Substring with One Unique Character \uD83D\uDD20\uD83D\uDCAA\n\nTo determine the "power" of a string, which is the maximum length of a non-empty substring that contains only one unique character, we can use a straightforward approach by iterating through the string and counting consecutive characters.\n\n## Approach\n1. Convert the string into a character array for easy access to individual characters.\n2. Initialize two variables: `c` to count the current sequence length of the same character, and `max` to keep track of the maximum sequence length found.\n3. Iterate through the string from the first to the second-to-last character.\n4. Compare the current character with the next one:\n   - If they are the same, increment `c`.\n   - If they are different, update `max` if `c` is greater than `max`, then reset `c` to 1.\n5. After the loop, return `max`.\n\nHere is the implementation:\n\n```java\nclass Solution {\n    public int maxPower(String s) {\n        int c = 1, max = 1;\n        char[] ch = s.toCharArray();\n        \n        for (int i = 0; i < s.length() - 1; i++) {\n            if (ch[i] == ch[i + 1]) {\n                c++;\n                max = Math.max(max, c);\n            } else {\n                c = 1;\n            }\n        }\n        \n        return max;\n    }\n}\n```\n![upvote.PNG](https://assets.leetcode.com/users/images/b1f4999b-b4e6-424a-8891-514bebc002b4_1720026195.5080085.png)\n\n## Complexity\n- **Time complexity:** \\(O(n)\\), where \\(n\\) is the length of the string. We iterate through the string once.\n- **Space complexity:** \\(O(1)\\), since we only use a few extra variables for counting and comparison. The space used for the character array is also \\(O(n)\\), but it is dependent on the input size, not an additional overhead.
['String', 'Java']