#3582
Easy Algorithms Generate tag for video caption
String Simulation
32.3% acceptance
Feb 25, 2026
67
30
Generate a hashtag for a video caption.
Rules: starts with '#', append words (first letter capitalized, rest lowercase),
but first word keeps all lowercase. Remove non-letters. Max 100 chars total.
If result is just '#' or longer than 100, return "".
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn generate_tag(caption: String) -> String {
let mut result = String::from("#");
let mut first_word = true;
for word in caption.split_whitespace() {
// Keep only letters
let filtered: String = word.chars().filter(|c| c.is_alphabetic()).collect();
if filtered.is_empty() {
continue;
}
if first_word {
result.push_str(&filtered.to_lowercase());
first_word = false;
} else {
let mut chars = filtered.chars();
if let Some(first) = chars.next() {
result.push(first.to_ascii_uppercase());
result.push_str(&chars.as_str().to_lowercase());
}
}
}
if result.len() > 100 {
result.truncate(100);
}
result
}
}