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

Given a string s, return the power of s.

Example 1:

Input: s = "leetcode " Output: 2 Explanation: The substring "ee " is of length 2 with the character 'e' only.

Example 2:

Input: s = "abbcccddddeeeeedcba " Output: 5 Explanation: The substring "eeeee " is of length 5 with the character 'e' only.

Constraints:

  • 1 <= s.length <= 500
  • s consists of only lowercase English letters.

Note: This problem is from LeetCode.
โœ… Compiled
โœ… Executed
โŒ Correct
program main
    implicit none

    integer :: power, i, j
    character(len=500) :: s

    ! Example 1
    s = "leetcode "
    power = powerOfString(s)
    write (*,*) power

    ! Example 2
    s = "abbcccddddeeeeedcba "
    power = powerOfString(s)
    write (*,*) power

contains

    function powerOfString(s) result(power)
        implicit none
        character(len=*), intent(in) :: s
        integer :: power, count, i

        power = 0
        do i = 1, len(s) - 1
            if (s(i:i+1) == s(i+1:i+2)) then
                cycle
            else
                count = 1
                do j = i + 1, len(s)
                    if (s(j:j+1) == s(i:i+1)) then
                        count = count + 1
                    else
                        exit
                    end if
                end do
                power = max(power, count)
            end if
        end do
    end function powerOfString

end program main
โŒ Compiled
โŒ Executed
โŒ Correct
module power_of_string

implicit none

contains

integer function power(s) result(power)

! Given a string s, return the power of s.

character(len=*), intent(in) :: s

integer :: i, j, count, max_count

power = 0

! Iterate over the string and calculate the power

do i = 1, len(s)

    count = 1

    ! Iterate over the remaining characters in the string

    do j = i + 1, len(s)

        ! If the current character is the same as the previous character, increment the count

        if (s(j:j) == s(i:i)) then

            count = count + 1

        ! If the current character is not the same as the previous character, break the loop

        else

            exit

        end if

    end do

    ! If the count is greater than the maximum count, update the maximum count and the index

    if (count > max_count) then

        max_count = count

        power = j - i + 1

    end if

end do

end function power

end module power_of_string

! Test the power function with the given examples

program test_power

use power_of_string

implicit none

integer :: power

! Example 1:

power = power_of_string("leetcode ")

write (*,*) "Example 1: Power of 'leetcode ' is ", power

! Example 2:

power = power_of_string("abbcccddddeeeeedcba ")

write (*,*) "Example 2: Power of 'abbcccddddeeeeedcba ' is ", power

end program test_power
๐ŸŒ Data from online sources
def angleClock(hour: int, minutes: int) -> float:
    minute_angle = 6 * minutes
    hour_angle = 30 * hour + 0.5 * minutes
    angle = abs(hour_angle - minute_angle)
    return min(angle, 360 - angle)

Here is the algorithm to find the smaller angle between the hour and the minute hand:

  1. Calculate the angle of the minute hand from the 12 o'clock position. Since each minute mark is 6 degrees apart (360/60), multiply the provided minutes by 6 to find the angle.
  2. Calculate the angle of the hour hand from the 12 o'clock position. Each hour mark is 30 degrees apart (360/12). Multiply the provided hours by 30, but also add half a degree for each minute passed as the hour hand moves slightly with each passing minute.
  3. Find the absolute difference between the hour and minute hand angles.
  4. Return the minimum of the calculated angle and 360 minus the calculated angle, as the target angle should always be the smaller angle formed between the clock hands.
๐ŸŒ Data from online sources
double angleClock(int hour, int minutes) {
    double minuteAngle = 6 * minutes;
    double hourAngle = 30 * hour + 0.5 * minutes;
    double angle = abs(hourAngle - minuteAngle);
    return min(angle, 360 - angle);
}

Here is the algorithm to find the smaller angle between the hour and the minute hand:

  1. Calculate the angle of the minute hand from the 12 o'clock position. Since each minute mark is 6 degrees apart (360/60), multiply the provided minutes by 6 to find the angle.
  2. Calculate the angle of the hour hand from the 12 o'clock position. Each hour mark is 30 degrees apart (360/12). Multiply the provided hours by 30, but also add half a degree for each minute passed as the hour hand moves slightly with each passing minute.
  3. Find the absolute difference between the hour and minute hand angles.
  4. Return the minimum of the calculated angle and 360 minus the calculated angle, as the target angle should always be the smaller angle formed between the clock hands.