3582. Generate Tag for Video Caption
Description
You are given a string caption
representing the caption for a video.
The following actions must be performed in order to generate a valid tag for the video:
-
Combine all words in the string into a single camelCase string prefixed with
'#'
. A camelCase string is one where the first letter of all words except the first one is capitalized. All characters after the first character in each word must be lowercase. -
Remove all characters that are not an English letter, except the first
'#'
. -
Truncate the result to a maximum of 100 characters.
Return the tag after performing the actions on caption
.
Example 1:
Input: caption = "Leetcode daily streak achieved"
Output: "#leetcodeDailyStreakAchieved"
Explanation:
The first letter for all words except "leetcode"
should be capitalized.
Example 2:
Input: caption = "can I Go There"
Output: "#canIGoThere"
Explanation:
The first letter for all words except "can"
should be capitalized.
Example 3:
Input: caption = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
Output: "#hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"
Explanation:
Since the first word has length 101, we need to truncate the last two letters from the word.
Constraints:
1 <= caption.length <= 150
caption
consists only of English letters and' '
.
Solutions
Solution 1: Simulation
We first split the title string into words, then process each word. The first word should be all lowercase, while for the subsequent words, the first letter is capitalized and the rest are lowercase. Next, we concatenate all the processed words and add a # symbol at the beginning. Finally, if the generated tag exceeds 100 characters in length, we truncate it to the first 100 characters.
The time complexity is \(O(n)\), and the space complexity is \(O(n)\), where \(n\) is the length of the title string.
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|