Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on core interview patterns fundamentals.
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 <= 100s consists of lowercase English letters.letter is a lowercase English letter.Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: General problem-solving
"foobar" "o"
"jjjj" "k"
sort-characters-by-frequency)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2278: Percentage of Letter in String
class Solution {
public int percentageLetter(String s, char letter) {
int cnt = 0;
for (char c : s.toCharArray()) {
if (c == letter) {
++cnt;
}
}
return cnt * 100 / s.length();
}
}
// Accepted solution for LeetCode #2278: Percentage of Letter in String
func percentageLetter(s string, letter byte) int {
return strings.Count(s, string(letter)) * 100 / len(s)
}
# Accepted solution for LeetCode #2278: Percentage of Letter in String
class Solution:
def percentageLetter(self, s: str, letter: str) -> int:
return s.count(letter) * 100 // len(s)
// Accepted solution for LeetCode #2278: Percentage of Letter in String
impl Solution {
pub fn percentage_letter(s: String, letter: char) -> i32 {
let count = s.chars().filter(|&c| c == letter).count();
(100 * count as i32 / s.len() as i32) as i32
}
}
// Accepted solution for LeetCode #2278: Percentage of Letter in String
function percentageLetter(s: string, letter: string): number {
const count = s.split('').filter(c => c === letter).length;
return Math.floor((100 * count) / s.length);
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.