Given a string s, the power of the string is the maximum length of a non-empty substring that contains only one unique character.

Return the power of the string.

Function Signature:

def string_power(s: str) -> int:
    """
    Calculate the power of the string.

    Parameters:
    - s: A string.

    Returns:
    - int: The power of the string.
    """
    # Your code here

Example:

s = "abbcccddddeeeeedcba"
result = string_power(s)
print(result)  # Output should be 5
# Explanation: The substring "eeeee" is of length 5 and contains only one unique character.

s = "triplepillooooow"
result = string_power(s)
print(result)  # Output should be 5
# Explanation: The substring "ooooo" is of length 5 and contains only one unique character.

s = "hooraaaaaaaaaaay"
result = string_power(s)
print(result)  # Output should be 11
# Explanation: The substring "aaaaaaaaaaa" is of length 11 and contains only one unique character.

s = "tourist"
result = string_power(s)
print(result)  # Output should be 1
# Explanation: Each character in the string is unique, so the maximum power is 1.

Note:

  • s consists of lowercase English letters only.

Instructions:

  • Implement the string_power function to solve the problem.
  • Use Python for your implementation.
  • Provide clear comments in your code to explain the logic.
  • Discuss the time and space complexity of your solution.

As always, we’ll share our solution to this problem tomorrow. Stay tuned 😊