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.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute-force approach calculates the percentage of a specific letter in a string by examining each character individually. It's like counting how many times you see that letter and then figuring out what percentage that number is of the total number of characters. We will go through the string one character at a time to solve this.
Here's how the algorithm would work step-by-step:
def percentage_of_letter_in_string(input_string, target_letter):
letter_count = 0
string_length = len(input_string)
# Iterate through the input string to count target letter occurrences.
for char in input_string:
if char == target_letter:
# Increment the counter when the current char matches target.
letter_count += 1
# Calculate percentage only if the string is not empty to avoid division by zero.
if string_length > 0:
percentage = (letter_count / string_length) * 100
return percentage
else:
return 0
The goal is to calculate the percentage of a specific letter within a given string. The optimal solution efficiently counts the occurrences of the letter and then uses a simple formula to determine the percentage, avoiding unnecessary computations or data structures.
Here's how the algorithm would work step-by-step:
def percentage_of_letter(string, letter):
letter_count = 0
# Need to count the occurances of the target letter.
for character in string:
if character == letter:
letter_count += 1
string_length = len(string)
# Avoid division by zero if the string is empty.
if string_length == 0:
return 0.0
# Calculate the percentage of the target letter.
percentage = (letter_count / string_length) * 100
return percentage
Case | How to Handle |
---|---|
Empty string s | Return 0 since the percentage of any character in an empty string is 0. |
Null string s | Throw IllegalArgumentException or return 0 (depending on requirements) to handle the invalid input. |
Null character letter | Throw IllegalArgumentException to indicate invalid input. |
Empty character letter | Treat it as a space or throw IllegalArgumentException based on requirements. |
String s with length 1 and letter not equal to s[0] | Return 0 since the letter does not appear in the string. |
String s with length 1 and letter equal to s[0] | Return 100 since the letter makes up 100% of the string. |
Very long string s to test scalability | The solution iterates through the string once, which scales linearly with string length O(n). |
String s consists entirely of the letter character | Return 100, indicating that the letter accounts for 100% of the string. |