LeetCode 2278: Percentage of Letter in String Solution

Master LeetCode problem 2278 (Percentage of Letter in String), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2278. Percentage of Letter in String

Easy

Problem Explanation

Explanation:

To solve this problem, we need to iterate through the given string s and count the number of occurrences of the character letter. Then we calculate the percentage of occurrences of letter in s and round it down to the nearest whole percentage.

  1. Initialize a variable count to store the number of occurrences of letter.
  2. Iterate through each character in the string s:
    • If the character is equal to letter, increment the count.
  3. Calculate the percentage of occurrences of letter in s by dividing count by the length of s and multiplying by 100.
  4. Return the rounded down value of the percentage.

:

Solution Code

class Solution {
    public int percentageOfLetterInString(String s, char letter) {
        int count = 0;
        for (char c : s.toCharArray()) {
            if (c == letter) {
                count++;
            }
        }
        return count * 100 / s.length();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2278 (Percentage of Letter in String)?

This page provides optimized solutions for LeetCode problem 2278 (Percentage of Letter in String) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2278 (Percentage of Letter in String)?

The time complexity for LeetCode 2278 (Percentage of Letter in String) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2278 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2278 in Java, C++, or Python.

Back to LeetCode Solutions