返回介绍

solution / 1800-1899 / 1805.Number of Different Integers in a String / README_EN

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

1805. Number of Different Integers in a String

中文文档

Description

You are given a string word that consists of digits and lowercase English letters.

You will replace every non-digit character with a space. For example, "a123bc34d8ef34" will become " 123  34 8  34". Notice that you are left with some integers that are separated by at least one space: "123", "34", "8", and "34".

Return _the number of different integers after performing the replacement operations on _word.

Two integers are considered different if their decimal representations without any leading zeros are different.

 

Example 1:

Input: word = "a123bc34d8ef34"
Output: 3
Explanation: The three different integers are "123", "34", and "8". Notice that "34" is only counted once.

Example 2:

Input: word = "leet1234code234"
Output: 2

Example 3:

Input: word = "a1b01c001"
Output: 1
Explanation: The three integers "1", "01", and "001" all represent the same integer because
the leading zeros are ignored when comparing their decimal values.

 

Constraints:

  • 1 <= word.length <= 1000
  • word consists of digits and lowercase English letters.

Solutions

Solution 1: Double Pointers + Simulation

Traverse the string word, find the start and end positions of each integer, cut out this substring, and store it in the hash set $s$.

After the traversal, return the size of the hash set $s$.

Note, the integer represented by each substring may be very large, we cannot directly convert it to an integer. Therefore, we can remove the leading zeros of each substring before storing it in the hash set.

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

class Solution:
  def numDifferentIntegers(self, word: str) -> int:
    s = set()
    i, n = 0, len(word)
    while i < n:
      if word[i].isdigit():
        while i < n and word[i] == '0':
          i += 1
        j = i
        while j < n and word[j].isdigit():
          j += 1
        s.add(word[i:j])
        i = j
      i += 1
    return len(s)
class Solution {
  public int numDifferentIntegers(String word) {
    Set<String> s = new HashSet<>();
    int n = word.length();
    for (int i = 0; i < n; ++i) {
      if (Character.isDigit(word.charAt(i))) {
        while (i < n && word.charAt(i) == '0') {
          ++i;
        }
        int j = i;
        while (j < n && Character.isDigit(word.charAt(j))) {
          ++j;
        }
        s.add(word.substring(i, j));
        i = j;
      }
    }
    return s.size();
  }
}
class Solution {
public:
  int numDifferentIntegers(string word) {
    unordered_set<string> s;
    int n = word.size();
    for (int i = 0; i < n; ++i) {
      if (isdigit(word[i])) {
        while (i < n && word[i] == '0') ++i;
        int j = i;
        while (j < n && isdigit(word[j])) ++j;
        s.insert(word.substr(i, j - i));
        i = j;
      }
    }
    return s.size();
  }
};
func numDifferentIntegers(word string) int {
  s := map[string]struct{}{}
  n := len(word)
  for i := 0; i < n; i++ {
    if word[i] >= '0' && word[i] <= '9' {
      for i < n && word[i] == '0' {
        i++
      }
      j := i
      for j < n && word[j] >= '0' && word[j] <= '9' {
        j++
      }
      s[word[i:j]] = struct{}{}
      i = j
    }
  }
  return len(s)
}
function numDifferentIntegers(word: string): number {
  return new Set(
    word
      .replace(/\D+/g, ' ')
      .trim()
      .split(' ')
      .filter(v => v !== '')
      .map(v => v.replace(/^0+/g, '')),
  ).size;
}
use std::collections::HashSet;
impl Solution {
  pub fn num_different_integers(word: String) -> i32 {
    let s = word.as_bytes();
    let n = s.len();
    let mut set = HashSet::new();
    let mut i = 0;
    while i < n {
      if s[i] >= b'0' && s[i] <= b'9' {
        let mut j = i;
        while j < n && s[j] >= b'0' && s[j] <= b'9' {
          j += 1;
        }
        while i < j - 1 && s[i] == b'0' {
          i += 1;
        }
        set.insert(String::from_utf8(s[i..j].to_vec()).unwrap());
        i = j;
      } else {
        i += 1;
      }
    }
    set.len() as i32
  }
}

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

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

发布评论

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