Problem Link

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:

  1. 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.

  2. Remove all characters that are not an English letter, except the first '#'.

  3. 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 ' '.

Solution


Python3

class Solution:
    def generateTag(self, caption: str) -> str:
        N = len(caption)
 
        s = caption.split()
        res = ["#"]
        count = 1
 
        for wordI, word in enumerate(s):
            for i, x in enumerate(word):
                if i == 0:
                    if wordI != 0:
                        res.append(x.upper())
                    else:
                        res.append(x.lower())
                else:
                    res.append(x.lower())
                    
                count += 1
 
                if count == 100: return "".join(res)
 
        return "".join(res)