返回介绍

solution / 2300-2399 / 2351.First Letter to Appear Twice / README_EN

发布于 2024-06-17 01:03:07 字数 6885 浏览 0 评论 0 收藏 0

2351. First Letter to Appear Twice

中文文档

Description

Given a string s consisting of lowercase English letters, return _the first letter to appear twice_.

Note:

  • A letter a appears twice before another letter b if the second occurrence of a is before the second occurrence of b.
  • s will contain at least one letter that appears twice.

 

Example 1:

Input: s = "abccbaacz"
Output: "c"
Explanation:
The letter 'a' appears on the indexes 0, 5 and 6.
The letter 'b' appears on the indexes 1 and 4.
The letter 'c' appears on the indexes 2, 3 and 7.
The letter 'z' appears on the index 8.
The letter 'c' is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.

Example 2:

Input: s = "abcdd"
Output: "d"
Explanation:
The only letter that appears twice is 'd' so we return 'd'.

 

Constraints:

  • 2 <= s.length <= 100
  • s consists of lowercase English letters.
  • s has at least one repeated letter.

Solutions

Solution 1: Array or Hash Table

We traverse the string $s$, using an array or hash table cnt to record the occurrence of each letter. When a letter appears twice, we return that letter.

The time complexity is $O(n)$ and the space complexity is $O(C)$. Here, $n$ is the length of the string $s$, and $C$ is the size of the character set. In this problem, $C = 26$.

class Solution:
  def repeatedCharacter(self, s: str) -> str:
    cnt = Counter()
    for c in s:
      cnt[c] += 1
      if cnt[c] == 2:
        return c
class Solution {
  public char repeatedCharacter(String s) {
    int[] cnt = new int[26];
    for (int i = 0;; ++i) {
      char c = s.charAt(i);
      if (++cnt[c - 'a'] == 2) {
        return c;
      }
    }
  }
}
class Solution {
public:
  char repeatedCharacter(string s) {
    int cnt[26]{};
    for (int i = 0;; ++i) {
      if (++cnt[s[i] - 'a'] == 2) {
        return s[i];
      }
    }
  }
};
func repeatedCharacter(s string) byte {
  cnt := [26]int{}
  for i := 0; ; i++ {
    cnt[s[i]-'a']++
    if cnt[s[i]-'a'] == 2 {
      return s[i]
    }
  }
}
function repeatedCharacter(s: string): string {
  const vis = new Array(26).fill(false);
  for (const c of s) {
    const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
    if (vis[i]) {
      return c;
    }
    vis[i] = true;
  }
  return ' ';
}
impl Solution {
  pub fn repeated_character(s: String) -> char {
    let mut vis = [false; 26];
    for &c in s.as_bytes() {
      if vis[(c - b'a') as usize] {
        return c as char;
      }
      vis[(c - b'a') as usize] = true;
    }
    ' '
  }
}
class Solution {
  /**
   * @param String $s
   * @return String
   */
  function repeatedCharacter($s) {
    for ($i = 0; ; $i++) {
      $hashtable[$s[$i]] += 1;
      if ($hashtable[$s[$i]] == 2) {
        return $s[$i];
      }
    }
  }
}
char repeatedCharacter(char* s) {
  int vis[26] = {0};
  for (int i = 0; s[i]; i++) {
    if (vis[s[i] - 'a']) {
      return s[i];
    }
    vis[s[i] - 'a']++;
  }
  return ' ';
}

Solution 2: Bit Manipulation

We can also use an integer mask to record whether each letter has appeared, where the $i$-th bit of mask indicates whether the $i$-th letter has appeared. When a letter appears twice, we return that letter.

The time complexity is $O(n)$ and the space complexity is $O(1)$. Here, $n$ is the length of the string $s$.

class Solution:
  def repeatedCharacter(self, s: str) -> str:
    mask = 0
    for c in s:
      i = ord(c) - ord('a')
      if mask >> i & 1:
        return c
      mask |= 1 << i
class Solution {
  public char repeatedCharacter(String s) {
    int mask = 0;
    for (int i = 0;; ++i) {
      char c = s.charAt(i);
      if ((mask >> (c - 'a') & 1) == 1) {
        return c;
      }
      mask |= 1 << (c - 'a');
    }
  }
}
class Solution {
public:
  char repeatedCharacter(string s) {
    int mask = 0;
    for (int i = 0;; ++i) {
      if (mask >> (s[i] - 'a') & 1) {
        return s[i];
      }
      mask |= 1 << (s[i] - 'a');
    }
  }
};
func repeatedCharacter(s string) byte {
  mask := 0
  for i := 0; ; i++ {
    if mask>>(s[i]-'a')&1 == 1 {
      return s[i]
    }
    mask |= 1 << (s[i] - 'a')
  }
}
function repeatedCharacter(s: string): string {
  let mask = 0;
  for (const c of s) {
    const i = c.charCodeAt(0) - 'a'.charCodeAt(0);
    if (mask & (1 << i)) {
      return c;
    }
    mask |= 1 << i;
  }
  return ' ';
}
impl Solution {
  pub fn repeated_character(s: String) -> char {
    let mut mask = 0;
    for &c in s.as_bytes() {
      if (mask & (1 << ((c - b'a') as i32))) != 0 {
        return c as char;
      }
      mask |= 1 << ((c - b'a') as i32);
    }
    ' '
  }
}
char repeatedCharacter(char* s) {
  int mask = 0;
  for (int i = 0; s[i]; i++) {
    if (mask & (1 << s[i] - 'a')) {
      return s[i];
    }
    mask |= 1 << s[i] - 'a';
  }
  return ' ';
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文