You are given a string s
and a character letter
. You need to return the percentage of characters in s
that are equal to letter
rounded down to the nearest whole percent.
s
?s
contain non-alphabetic characters?s
and count how many times the character letter
appears.letter
by the length of the string s
, multiplying by 100, and then using integer division to round down to the nearest whole number.s
is empty, the function should handle it appropriately, although typically this edge case may not be needed if input constraints guarantee a non-empty string.def percentageLetter(s: str, letter: str) -> int:
# Count occurrences of the specified letter in the string
count = s.count(letter)
# Calculate the percentage and round down to the nearest integer
percentage = (count * 100) // len(s)
return percentage
n
is the length of the string s
. This is because the count
method in Python traverses the entire string to count occurrences of letter
.This strategy effectively counts the number of occurrences and calculates the percentage in a straightforward manner, ensuring both efficiency and simplicity.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?