Percentage of Letter in String

🏠 ⬅️ ➡️

Given a string s and a character letter, return the percentage of characters in s that equal letter rounded down to the nearest whole percent.

Example 1:

Input: s = "foobar ", letter = "o " Output: 33 Explanation: The percentage of characters in s that equal the letter 'o' is 2 / 6 * 100% = 33% when rounded down, so we return 33.

Example 2:

Input: s = "jjjj ", letter = "k " Output: 0 Explanation: The percentage of characters in s that equal the letter 'k' is 0%, so we return 0.

Constraints:

  • 1 <= s.length <= 100
  • s consists of lowercase English letters.
  • letter is a lowercase English letter.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    character(len=*), parameter :: s = "foobar "
    character, parameter :: letter = "o"
    integer :: result

    result = solve(s, letter)
    print "(I0)", result

contains

    function solve(s, letter) result(percentage)
        implicit none
        character(len=*), intent(in) :: s
        character, intent(in) :: letter
        integer :: i, n, percentage

        n = len(s)
        do i = 1, n
            if (s(i:i) == letter) then
                percentage = percentage + 1
            end if
        end do
        percentage = percentage * 100 / n
        percentage = int(percentage, kind=4)
    end function solve
end program main
Compiled
Executed
Correct
program main
      implicit none

      integer :: i, j, n, len
      character(len=*) :: s, letter
      real :: percentage

      ! Read input
      read(*,*) s
      read(*,*) letter

      ! Calculate percentage
      len = len(s)
      n = 0
      do i = 1, len
          if (s(i:i) == letter) then
              n = n + 1
          end if
      end do
      percentage = real(n) / real(len) * 100.0
      percentage = int(percentage)

      ! Output result
      write(*,*) percentage

      end program main
🌐 Data from online sources
def three_consecutive_integers(num):
    if num % 3 == 0:
        return [num // 3 - 1, num // 3, num // 3 + 1]
    elif num % 3 == 1:
        return [(num - 2) // 3, (num + 1) // 3, (num + 4) // 3]
    else:
        return []

The algorithm first checks if the number num is divisible by 3. If it is, the three consecutive integers must be {num / 3 - 1, num / 3, num / 3 + 1}. If num is one more than a multiple of 3 (i.e., num % 3 == 1), then the three consecutive integers are {(num - 2) / 3, (num + 1) / 3, (num + 4) / 3}. If num is neither divisible by 3 nor one more than a multiple of 3 (i.e., num % 3 == 2), it cannot be expressed as the sum of three consecutive integers, so the function returns an empty array.

🌐 Data from online sources
#include <vector>

std::vector<int> threeConsecutiveIntegers(int num) {
    if (num % 3 == 0) {
        return {num / 3 - 1, num / 3, num / 3 + 1};
    } else if (num % 3 == 1) {
        return {(num - 2) / 3, (num + 1) / 3, (num + 4) / 3};
    } else {
        return {};
    }
}

The algorithm first checks if the number num is divisible by 3. If it is, the three consecutive integers must be {num / 3 - 1, num / 3, num / 3 + 1}. If num is one more than a multiple of 3 (i.e., num % 3 == 1), then the three consecutive integers are {(num - 2) / 3, (num + 1) / 3, (num + 4) / 3}. If num is neither divisible by 3 nor one more than a multiple of 3 (i.e., num % 3 == 2), it cannot be expressed as the sum of three consecutive integers, so the function returns an empty array.