Advertisement

Percentage of Letter in String - LeetCode 2278 Solution

Percentage of Letter in String - Complete Solution Guide

Percentage of Letter in String is LeetCode problem 2278, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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.

Detailed Explanation

The problem asks you to calculate the percentage of times a specific character appears in a given string. The input consists of a string `s` (containing lowercase English letters) and a character `letter` (also a lowercase English letter). The output should be the percentage of characters in `s` that are equal to `letter`, rounded down to the nearest whole number. For example, if `s = "foobar"` and `letter = "o"`, the output should be 33 because 'o' appears twice in a string of length 6, and (2/6) * 100 = 33.33..., which rounds down to 33.

Solution Approach

The provided solutions use a straightforward iterative approach. They iterate through each character of the input string `s`. If a character matches the input `letter`, a counter is incremented. After iterating through the entire string, the percentage is calculated by multiplying the count by 100 and dividing by the string's length. Integer division is used to ensure the result is rounded down. The Python and C solutions explicitly use integer division (`//` in Python, `/` in C after casting to integer), while Java and C++ rely on implicit type conversion and casting to int to achieve the same result.

Step-by-Step Algorithm

  1. Step 1: Initialize a counter variable `count` to 0.
  2. Step 2: Iterate through each character `char` in the input string `s`.
  3. Step 3: If `char` is equal to the input `letter`, increment `count`.
  4. Step 4: After the loop, calculate the percentage: `(count * 100) / len(s)` (Python and C) or `(int)(((double)count / s.length()) * 100)` (Java and C++).
  5. Step 5: Return the calculated percentage.

Key Insights

  • Insight 1: The solution requires iterating through the string to count the occurrences of the target character.
  • Insight 2: Integer division is crucial for rounding down the percentage to the nearest whole number.
  • Insight 3: Handling the edge case of an empty string or a string where the target character doesn't appear is important to avoid division by zero errors.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(1)

Topics

This problem involves: String.

Companies

Asked at: American Express.