Skip to content

第一个出现两次的字母

仲灏2022-11-14约 1 分钟

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

第一个出现两次的字母

CategoryDifficultyLikesDislikes
algorithmsEasy (84.41%)13-

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

给你一个由小写英文字母组成的字符串 s ,请你找出并返回第一个出现 两次 的字母。

注意:

  • 如果 a第二次 出现比 b第二次 出现在字符串中的位置更靠前,则认为字母 a 在字母 b 之前出现两次。
  • s 包含至少一个出现两次的字母。

示例 1:

输入:s = "abccbaacz"
输出:"c"
解释:
字母 'a' 在下标 0 、5 和 6 处出现。
字母 'b' 在下标 1 和 4 处出现。
字母 'c' 在下标 2 、3 和 7 处出现。
字母 'z' 在下标 8 处出现。
字母 'c' 是第一个出现两次的字母,因为在所有字母中,'c' 第二次出现的下标是最小的。

示例 2:

输入:s = "abcdd"
输出:"d"
解释:
只有字母 'd' 出现两次,所以返回 'd' 。

提示:

  • 2 <= s.length <= 100
  • s 由小写英文字母组成
  • s 包含至少一个重复字母

Discussion | Solution


typescript
/*
 * @Author: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Date: 2022-11-14 15:26:40
 * @LastEditTime: 2022-11-14 15:30:14
 * @LastEditors: 仲灏<izhaong@outlook.com>🌶🌶🌶
 * @Description:  
 * @FilePath: /面试题1/Users/izhaong/izhaong/Project_me/leetcode/2351.第一个出现两次的字母.ts
 */
/*
 * @lc app=leetcode.cn id=2351 lang=typescript
 *
 * [2351] 第一个出现两次的字母
 *
 * https://leetcode.cn/problems/first-letter-to-appear-twice/description/
 *
 * algorithms
 * Easy (84.41%)
 * Likes:    13
 * Dislikes: 0
 * Total Accepted:    13.4K
 * Total Submissions: 15.9K
 * Testcase Example:  '"abccbaacz"'
 *
 * 给你一个由小写英文字母组成的字符串 s ,请你找出并返回第一个出现 两次 的字母。
 * 
 * 注意:
 * 
 * 
 * 如果 a 的 第二次 出现比 b 的 第二次 出现在字符串中的位置更靠前,则认为字母 a 在字母 b 之前出现两次。
 * s 包含至少一个出现两次的字母。
 * 
 * 
 * 
 * 
 * 示例 1:
 * 
 * 输入:s = "abccbaacz"
 * 输出:"c"
 * 解释:
 * 字母 'a' 在下标 0 、5 和 6 处出现。
 * 字母 'b' 在下标 1 和 4 处出现。
 * 字母 'c' 在下标 2 、3 和 7 处出现。
 * 字母 'z' 在下标 8 处出现。
 * 字母 'c' 是第一个出现两次的字母,因为在所有字母中,'c' 第二次出现的下标是最小的。
 * 
 * 
 * 示例 2:
 * 
 * 输入:s = "abcdd"
 * 输出:"d"
 * 解释:
 * 只有字母 'd' 出现两次,所以返回 'd' 。
 * 
 * 
 * 
 * 
 * 提示:
 * 
 * 
 * 2 <= s.length <= 100
 * s 由小写英文字母组成
 * s 包含至少一个重复字母
 * 
 * 
 */

// @lc code=start
function repeatedCharacter(s: string): string {
    let had = ''
    let res = ''
    for (let i = 0; i < s.length; i++) {
        const e = s[i];
        if(had.indexOf(e) !== -1) {
            res = e
            break;
        }
        had += e
    }
    return res
};
// @lc code=end