LeetCode 题解工作台

为视频标题生成标签

给你一个字符串 caption ,表示一个视频的标题。 需要按照以下步骤 按顺序 生成一个视频的 有效标签 : 将 所有单词 组合为单个 驼峰命名字符串 ,并在前面加上 '#' 。 驼峰命名字符串 指的是除第一个单词外,其余单词的首字母大写,且每个单词的首字母之后的字符必须是小写。 移除 所有不是英…

category

2

题型

code_blocks

5

代码语言

hub

3

相关题

当前训练重点

简单 · string·结合·模拟

bolt

答案摘要

我们首先将标题字符串分割成单词,然后对每个单词进行处理。第一个单词需要全部小写,后续的单词首字母大写,其余部分小写。接着,我们将所有处理后的单词连接起来,并在前面加上 `#` 符号。最后,如果生成的标签长度超过 100 个字符,则截断为前 100 个字符。 时间复杂度 ,空间复杂度 ,其中 是标题字符串的长度。

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 string·结合·模拟 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

给你一个字符串 caption,表示一个视频的标题。

需要按照以下步骤 按顺序 生成一个视频的 有效标签 

  1. 所有单词 组合为单个 驼峰命名字符串 ,并在前面加上 '#'驼峰命名字符串 指的是除第一个单词外,其余单词的首字母大写,且每个单词的首字母之后的字符必须是小写。

  2. 移除 所有不是英文字母的字符,但 保留 第一个字符 '#'

  3. 将结果 截断 为最多 100 个字符。

caption 执行上述操作后,返回生成的 标签 

 

示例 1:

输入: caption = "Leetcode daily streak achieved"

输出: "#leetcodeDailyStreakAchieved"

解释:

除了 "leetcode" 以外的所有单词的首字母需要大写。

示例 2:

输入: caption = "can I Go There"

输出: "#canIGoThere"

解释:

除了 "can" 以外的所有单词的首字母需要大写。

示例 3:

输入: caption = "hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

输出: "#hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh"

解释:

由于第一个单词长度为 101,因此需要从单词末尾截去最后两个字符。

 

提示:

  • 1 <= caption.length <= 150
  • caption 仅由英文字母和 ' ' 组成。
lightbulb

解题思路

方法一:模拟

我们首先将标题字符串分割成单词,然后对每个单词进行处理。第一个单词需要全部小写,后续的单词首字母大写,其余部分小写。接着,我们将所有处理后的单词连接起来,并在前面加上 # 符号。最后,如果生成的标签长度超过 100 个字符,则截断为前 100 个字符。

时间复杂度 O(n)O(n),空间复杂度 O(n)O(n),其中 nn 是标题字符串的长度。

1
2
3
4
5
6
7
class Solution:
    def generateTag(self, caption: str) -> str:
        words = [s.capitalize() for s in caption.split()]
        if words:
            words[0] = words[0].lower()
        return "#" + "".join(words)[:99]
speed

复杂度分析

指标
时间complexity is O(n) where n is the length of the caption because each character is processed once. Space complexity is O(n) for storing words and the final tag string.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Check how you handle the first word versus subsequent words for capitalization.

  • question_mark

    Confirm proper truncation behavior when the tag length exceeds allowed limits.

  • question_mark

    Ask how the simulation of camel-case formatting is implemented efficiently.

warning

常见陷阱

外企场景
  • error

    Forgetting to lowercase the first word, which violates the hashtag style.

  • error

    Incorrectly capitalizing words that should remain lowercase or skipping truncation.

  • error

    Concatenating words without handling spaces, which breaks the simulation.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Allow numeric characters in captions and simulate proper capitalization with numbers.

  • arrow_right_alt

    Support multiple spaces between words and ensure the tag ignores extra spaces.

  • arrow_right_alt

    Generate hashtags using different capitalization rules, like all uppercase after the first word.

help

常见问题

外企场景

为视频标题生成标签题解:string·结合·模拟 | LeetCode #3582 简单