🔍Very Easy and Optimal Java Solution! 🚀
Intuition\n\n\nThe problem asks for the maximum number of consecutive occurrences of the same character within a given string. To solve this, we can iterate thr
# Intuition\n![Screenshot 2024-09-17 040938.png](https://assets.leetcode.com/users/images/ed5bcb98-d57a-4e90-bbf5-3028d37508bc_1726528371.983063.png)\n\nThe problem asks for the maximum number of consecutive occurrences of the same character within a given string. To solve this, we can iterate through the string and keep track of the current character and its consecutive count. If we encounter a different character, we update the maximum count and reset the consecutive count for the new character.\n\n# Approach\nInitialization:\n\nInitialize a variable power to 1 (for the first character) and a variable pow to 0 to store the maximum consecutive count.\nInitialize a variable c (for the previous character) with a space or any value that is not in the given string.\nIteration:\n\nIterate through the string s.\nFor each character c1:\nIf c1 is the same as the previous character c, increment power.\nOtherwise, set power to 1 (for the new character) and update c to c1.\nUpdating Maximum Count:\n\nAfter each iteration, update pow to the maximum value between the current power and the previous pow.\nReturn:\n\nReturn the final value of pow, which represents the maximum number of consecutive occurrences.\n\n# Complexity\n- Time complexity:\n O(n), where n is the length of the string. We iterate through the string once.\n\n- Space complexity:\nO(1) since we are only using a few constant-sized variables.\n\n# Code\n```java []\nclass Solution {\n    public int maxPower(String s) {\n        int power=1;\n        int c=\' \';\n        int pow=0;\n        for(int i=0;i<s.length();i++){\n            char c1=s.charAt(i);\n            if(c1==c){\n                power++;\n            }else\n                power=1;\n            c=c1;\n            pow=Math.max(pow,power);\n            \n        }\n        return pow;\n    }\n}\n```
['Java']