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.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
1 1
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
temp.f95:7:39: 7 | integer function power(s) result(power) | 1 Error: RESULT variable at (1) must be different than function name temp.f95:11:33: 11 | character(len=*), intent(in) :: s | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:33: 13 | integer :: i, j, count, max_count | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:6: 15 | power = 0 | 1 Error: Symbol โpowerโ at (1) has already been host associated temp.f95:19:16: 19 | do i = 1, len(s) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:21:13: 21 | count = 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:25:24: 25 | do j = i + 1, len(s) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:29:16: 29 | if (s(j:j) == s(i:i)) then | 1 Error: Syntax error in argument list at (1) temp.f95:31:29: 31 | count = count + 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:35:12: 35 | else | 1 Error: Unexpected ELSE statement in CONTAINS section at (1) temp.f95:37:16: 37 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:39:11: 39 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:41:7: 41 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:45:31: 45 | if (count > max_count) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:47:25: 47 | max_count = count | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:49:14: 49 | power = j - i + 1 | 1 Error: Symbol โpowerโ at (1) has already been host associated temp.f95:51:7: 51 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:53:3: 53 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:55:3: 55 | end function power | 1 Error: Expecting END MODULE statement at (1) temp.f95:63:5: 63 | use power_of_string | 1 Fatal Error: Cannot open module file โpower_of_string.modโ for reading at (1): No such file or directory compilation terminated.
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:
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: