Generate Tag for Video Caption - Problem
Generate Tag for Video Caption
You're building a social media platform where users upload videos with captions. To help with content discovery and trending algorithms, you need to automatically generate hashtags from video captions.
Given a string
1. Convert to camelCase: Combine all words into a single string where the first word is lowercase and subsequent words have their first letter capitalized
2. Add hashtag prefix: Prefix the result with '#'
3. Remove non-English letters: Keep only English letters (a-z, A-Z) and the '#' symbol
4. Truncate: Limit the final result to maximum 100 characters
Example:
Return the generated hashtag after applying all transformations.
You're building a social media platform where users upload videos with captions. To help with content discovery and trending algorithms, you need to automatically generate hashtags from video captions.
Given a string
caption representing the caption for a video, transform it into a valid hashtag by performing these operations in order:1. Convert to camelCase: Combine all words into a single string where the first word is lowercase and subsequent words have their first letter capitalized
2. Add hashtag prefix: Prefix the result with '#'
3. Remove non-English letters: Keep only English letters (a-z, A-Z) and the '#' symbol
4. Truncate: Limit the final result to maximum 100 characters
Example:
"Hello World! 123" โ "#helloWorld"Return the generated hashtag after applying all transformations.
Input & Output
example_1.py โ Basic camelCase conversion
$
Input:
caption = "Hello World"
โบ
Output:
"#helloWorld"
๐ก Note:
Convert to camelCase (hello + World), add # prefix, no special characters to remove, result is under 100 chars
example_2.py โ With special characters
$
Input:
caption = "Amazing Video! 2024"
โบ
Output:
"#amazingVideo"
๐ก Note:
Convert to camelCase (amazing + Video), add # prefix, remove '!', '2024', and spaces, result is '#amazingVideo'
example_3.py โ Long caption truncation
$
Input:
caption = "This Is A Very Long Video Caption That Should Be Truncated Because It Exceeds The Maximum Character Limit"
โบ
Output:
"#thisIsAVeryLongVideoCaptionThatShouldBeTruncatedBecauseItExceedsTheMaximumCharacterLimit"
๐ก Note:
Convert to camelCase, add # prefix, no special characters, but result would be longer than 100 characters so it gets truncated at position 100
Visualization
Tap to expand
Understanding the Visualization
1
Start with #
Initialize result with hashtag symbol
2
Process First Word
Convert first word to all lowercase
3
Handle Word Boundaries
Detect spaces to identify new words
4
Capitalize New Words
Make first letter of subsequent words uppercase
5
Filter Characters
Skip numbers and special characters
6
Check Length Limit
Stop at 100 characters to meet platform requirements
Key Takeaway
๐ฏ Key Insight: Process everything in one pass - track word boundaries with a boolean flag, apply camelCase formatting on-the-fly, filter unwanted characters, and stop at 100 characters for optimal O(n) performance.
Time & Space Complexity
Time Complexity
O(n)
Single pass through input string, each character processed once
โ Linear Growth
Space Complexity
O(1)
Only the result string builder, no intermediate data structures needed
โ Linear Space
Constraints
- 1 โค caption.length โค 1000
- caption consists of English letters, spaces, digits, and punctuation marks
- The result must not exceed 100 characters
- At least one English letter must be present in the caption
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code