-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1805-NumberOfDifferentIntegersInAString.go
More file actions
86 lines (73 loc) · 2.81 KB
/
1805-NumberOfDifferentIntegersInAString.go
File metadata and controls
86 lines (73 loc) · 2.81 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
// 1805. Number of Different Integers in a String
// 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.
import "fmt"
import "regexp"
func numDifferentIntegers(word string) int {
re := regexp.MustCompile(`0*(\d+)`)
mp := make(map[string]bool)
for _, v := range re.FindAllStringSubmatch(word, -1) {
mp[v[1]] = true
}
return len(mp)
}
func numDifferentIntegers1(word string) int {
n, start, mp := len(word), 0, make(map[string]bool)
isDigit := func(ch byte) bool { return ch >= '0' && ch <= '9' }
for {
for start < n && !isDigit(word[start]) {
start++
}
if start == n { break }
end := start
for end < n && isDigit(word[end]) {
end++
}
for end - start > 1 && word[start] == '0' { // 去除前导 0
start++
}
mp[word[start:end]] = true
start = end
}
return len(mp)
}
func main() {
// Example 1:
// Input: word = "a123bc34d8ef34"
// Output: 3
// Explanation: The three different integers are "123", "34", and "8". Notice that "34" is only counted once.
fmt.Println(numDifferentIntegers("a123bc34d8ef34")) // 3
// Example 2:
// Input: word = "leet1234code234"
// Output: 2
fmt.Println(numDifferentIntegers("leet1234code234")) // 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.
fmt.Println(numDifferentIntegers("a1b01c001")) // 1
fmt.Println(numDifferentIntegers1("a123bc34d8ef34")) // 3
fmt.Println(numDifferentIntegers1("leet1234code234")) // 2
fmt.Println(numDifferentIntegers1("a1b01c001")) // 1
}