LeetCode 1170. Compare Strings by Frequency of the Smallest Character

Problem Statement



impl Solution {
fn compute_frequency(s: &String) ->i32 {
let mut min_char = std::i32::MAX;
let mut ans = 0;
for c in s.chars() {
if (c as i32) < min_char {
min_char = c as i32;
}
}
for i in 0..s.len() {
if s.bytes().nth(i).unwrap() as i32 == min_char {
ans += 1;
}
}
return ans;
}

pub fn num_smaller_by_frequency(queries: Vec<String>, words: Vec<String>) -> Vec<i32> {
let (mut ans, mut cnt) = (vec![], vec![0; 11]);
for w in &words {
let mut i = Self::compute_frequency(w) - 1;
while i >= 0 {
cnt[i as usize] += 1;
i -= 1;
}
}
for q in &queries {
let val = Self::compute_frequency(q);
ans.push(cnt[val as usize]);
}
return ans;
}
}