Skip to content

句子中的最多单词数

仲灏2022-11-08约 1 分钟

<div style="display: none;" hidden="true" aria-hidden="true" data-nosnippet>Are you an LLM? You can read better optimized documentation at /pages/6867b8.md for this page in Markdown format</div>

句子中的最多单词数

CategoryDifficultyLikesDislikes
algorithmsEasy (85.76%)16-

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Tags</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><a href="https://leetcode.com/tag/Unknown" title="https://leetcode.com/tag/Unknown" style="color: var(--vscode-textLink-foreground); text-decoration: none;"><code style="color: var(--vscode-textLink-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></a></p></details>

<details style="color: rgb(225, 228, 232); font-family: -apple-system, "system-ui", "Segoe WPC", "Segoe UI", system-ui, Ubuntu, "Droid Sans", sans-serif; font-size: 14px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial;"><summary><strong>Companies</strong></summary><p style="margin-top: 0px; margin-bottom: 0.7em;"><code style="color: var(--vscode-textPreformat-foreground); font-family: var(--vscode-editor-font-family, "SF Mono", Monaco, Menlo, Consolas, "Ubuntu Mono", "Liberation Mono", "DejaVu Sans Mono", "Courier New", monospace); font-size: 1em; line-height: 1.357em; white-space: pre-wrap;"></code></p></details>

一个 句子 由一些 单词 以及它们之间的单个空格组成,句子的开头和结尾不会有多余空格。

给你一个字符串数组 sentences ,其中 sentences[i] 表示单个 句子

请你返回单个句子里 单词的最多数目

示例 1:

输入:sentences = ["alice and bob love leetcode", "i think so too", "this is great thanks very much"]
输出:6
解释:
- 第一个句子 "alice and bob love leetcode" 总共有 5 个单词。
- 第二个句子 "i think so too" 总共有 4 个单词。
- 第三个句子 "this is great thanks very much" 总共有 6 个单词。
所以,单个句子中有最多单词数的是第三个句子,总共有 6 个单词。

示例 2:

输入:sentences = ["please wait", "continue to fight", "continue to win"]
输出:3
解释:可能有多个句子有相同单词数。
这个例子中,第二个句子和第三个句子(加粗斜体)有相同数目的单词数。

提示:

  • 1 <= sentences.length <= 100
  • 1 <= sentences[i].length <= 100
  • sentences[i] 只包含小写英文字母和 ' '
  • sentences[i] 的开头和结尾都没有空格。
  • sentences[i] 中所有单词由单个空格隔开。

Discussion | Solution

tsx
/*
 * @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Date: 2022-11-08 10:49:33
 * @LastEditTime: 2022-11-08 10:57:23
 * @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Description:  
 * @FilePath: /loan-home/Users/izhaong/izhaong/Project_me/leetcode/2114.句子中的最多单词数.ts
 */
/*
 * @lc app=leetcode.cn id=2114 lang=typescript
 *
 * [2114] 句子中的最多单词数
 *
 * https://leetcode.cn/problems/maximum-number-of-words-found-in-sentences/description/
 *
 * algorithms
 * Easy (85.76%)
 * Likes:    16
 * Dislikes: 0
 * Total Accepted:    15.5K
 * Total Submissions: 18K
 * Testcase Example:  '["alice and bob love leetcode","i think so too","this is great thanks very much"]'
 *
 * 一个 句子 由一些 单词 以及它们之间的单个空格组成,句子的开头和结尾不会有多余空格。
 * 
 * 给你一个字符串数组 sentences ,其中 sentences[i] 表示单个 句子 。
 * 
 * 请你返回单个句子里 单词的最多数目 。
 * 
 * 
 * 
 * 示例 1:
 * 
 * 输入:sentences = ["alice and bob love leetcode", "i think so too", "this is
 * great thanks very much"]
 * 输出:6
 * 解释:
 * - 第一个句子 "alice and bob love leetcode" 总共有 5 个单词。
 * - 第二个句子 "i think so too" 总共有 4 个单词。
 * - 第三个句子 "this is great thanks very much" 总共有 6 个单词。
 * 所以,单个句子中有最多单词数的是第三个句子,总共有 6 个单词。
 * 
 * 
 * 示例 2:
 * 
 * 输入:sentences = ["please wait", "continue to fight", "continue to win"]
 * 输出:3
 * 解释:可能有多个句子有相同单词数。
 * 这个例子中,第二个句子和第三个句子(加粗斜体)有相同数目的单词数。
 * 
 * 
 * 
 * 
 * 提示:
 * 
 * 
 * 1 <= sentences.length <= 100
 * 1 <= sentences[i].length <= 100
 * sentences[i] 只包含小写英文字母和 ' ' 。
 * sentences[i] 的开头和结尾都没有空格。
 * sentences[i] 中所有单词由单个空格隔开。
 * 
 * 
 */

// @lc code=start
function mostWordsFound(sentences: string[]): number {
    const sentencesLen = sentences.length
    let maxLen = 0
    for (let i = 0; i < sentencesLen; i++) {
        const words = sentences[i].split(' ')
        const wordsLen = words.length
        maxLen = Math.max(maxLen, wordsLen)
    }
    return maxLen
};
// @lc code=end