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 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
๐ŸŽฌ Video Caption to Hashtag ConverterTransform "Hello World! 123" โ†’ "#helloWorld"#STARTHello โ†’helloLOWERCASEโŽตSPACEWorld โ†’WorldCAPITALIZE! 123 โ†’skipFILTERResult Building Process# โ†’ #h โ†’ #he โ†’ #hel โ†’ #hell โ†’ #hello โ†’ #helloW โ†’ #helloWo โ†’ #helloWor โ†’ #helloWorl โ†’ #helloWorldCharacters processed one by one, applying all rules simultaneouslyFinal Output: "#helloWorld"โœ… Ready for social media posting!
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

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only the result string builder, no intermediate data structures needed

n
2n
โœ“ 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
Asked in
Meta 45 TikTok 38 Instagram 32 YouTube 28 Twitter 25
58.2K Views
Medium Frequency
~15 min Avg. Time
2.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen