Beats 100% of users with C++ || Step By Step Explain || Best Approach || Easy to Understand ||
Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it.....\n\n---\n\n# Intuition\n Describe your first thoughts on how to solve this proble
# Abhiraj Pratap Singh\n\n---\n\n# if you like the solution please UPVOTE it.....\n\n---\n\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- Traverse the string while keeping track of consecutive counts of repeating characters.\n\n---\n\n![1713037055032.png](https://assets.leetcode.com/users/images/c70db5ac-a664-463e-b4af-f8f64fc084e9_1713721220.5365336.png)\n\n---\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Initialize variables ans and count to 1.\n- Iterate through the string starting from index 1.\n- If the current character is equal to the previous character, increment count.\n- If the current character is different from the previous character:\n    - Update ans to be the maximum of ans and count.\n    - Reset count to 1.\n- After the loop, compare ans and count and return the maximum value.\n\n---\n\n# Complexity\n- **Time complexity :** O(n), where n is the length of the input string. This is because the algorithm iterates through the string once.\n- **Space complexity :** O(1). The algorithm uses a constant amount of extra space regardless of the input size.\n\n---\n\n# Code\n```\nclass Solution {\npublic:\n    int maxPower(string s) \n    {\n        int ans =  1 ;\n        int count = 1 ;\n        for ( int i = 1 ; i < s.length() ; i++ )\n        {\n            if ( s[i] == s[i-1] )\n                count++ ;\n            else\n            {\n                ans = max ( ans , count ) ;\n                count = 1 ;\n            }\n        }\n        if ( ans < count ) \n            return count ;\n        return ans ;\n    }\n};\n```\n\n---\n\n# if you like the solution please UPVOTE it.....\n\n---\n\n![55aad399-a443-4ca5-8a2a-791080811cba_1710608903.696966.gif](https://assets.leetcode.com/users/images/440f393e-0451-40b6-9651-9db42514c564_1713721412.5438173.gif)\n\n\n![7f522c1f-b96f-4a71-8ca2-85bd583a01aa_1710608865.388014.gif](https://assets.leetcode.com/users/images/b1d30aa3-ff61-4403-ac8d-c224a26487f7_1713721427.480837.gif)\n\n\n---\n\n---
['String', 'C++']