跳转至

3582. 为视频标题生成标签

题目描述

给你一个字符串 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 仅由英文字母和 ' ' 组成。

解法

方法一:模拟

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

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

1
2
3
4
5
6
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]
 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
class Solution {
    public String generateTag(String caption) {
        String[] words = caption.trim().split("\\s+");
        StringBuilder sb = new StringBuilder("#");

        for (int i = 0; i < words.length; i++) {
            String word = words[i];
            if (word.isEmpty()) {
                continue;
            }

            word = word.toLowerCase();
            if (i == 0) {
                sb.append(word);
            } else {
                sb.append(Character.toUpperCase(word.charAt(0)));
                if (word.length() > 1) {
                    sb.append(word.substring(1));
                }
            }

            if (sb.length() >= 100) {
                break;
            }
        }

        return sb.length() > 100 ? sb.substring(0, 100) : sb.toString();
    }
}
 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
class Solution {
public:
    string generateTag(string caption) {
        istringstream iss(caption);
        string word;
        ostringstream oss;
        oss << "#";
        bool first = true;
        while (iss >> word) {
            transform(word.begin(), word.end(), word.begin(), ::tolower);
            if (first) {
                oss << word;
                first = false;
            } else {
                word[0] = toupper(word[0]);
                oss << word;
            }
            if (oss.str().length() >= 100) {
                break;
            }
        }

        string ans = oss.str();
        if (ans.length() > 100) {
            ans = ans.substr(0, 100);
        }
        return ans;
    }
};
 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
func generateTag(caption string) string {
    words := strings.Fields(caption)
    var builder strings.Builder
    builder.WriteString("#")

    for i, word := range words {
        word = strings.ToLower(word)
        if i == 0 {
            builder.WriteString(word)
        } else {
            runes := []rune(word)
            if len(runes) > 0 {
                runes[0] = unicode.ToUpper(runes[0])
            }
            builder.WriteString(string(runes))
        }
        if builder.Len() >= 100 {
            break
        }
    }

    ans := builder.String()
    if len(ans) > 100 {
        ans = ans[:100]
    }
    return ans
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
function generateTag(caption: string): string {
    const words = caption.trim().split(/\s+/);
    let ans = '#';
    for (let i = 0; i < words.length; i++) {
        const word = words[i].toLowerCase();
        if (i === 0) {
            ans += word;
        } else {
            ans += word.charAt(0).toUpperCase() + word.slice(1);
        }
        if (ans.length >= 100) {
            ans = ans.slice(0, 100);
            break;
        }
    }
    return ans;
}

评论