-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCCI0101-IsUnique.go
More file actions
52 lines (42 loc) · 1014 Bytes
/
LCCI0101-IsUnique.go
File metadata and controls
52 lines (42 loc) · 1014 Bytes
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
package main
// 面试题 01.01. Is Unique LCCI
// Implement an algorithm to determine if a string has all unique characters.
// What if you cannot use additional data structures?
// Example 1:
// Input: s = "leetcode"
// Output: false
// Example 2:
// Input: s = "abc"
// Output: true
// Note:
// 0 <= len(s) <= 100
import "fmt"
// map
func isUnique(str string) bool {
m := make(map[byte]int)
for i := 0; i < len(str); i += 1 {
m[str[i]] += 1
// 如果大于 1 说明有重复出现
if m[str[i]] > 1 {
return false
}
}
return true
}
// array
func isUnique1(astr string) bool {
cnt := [26]byte{}
for _, ch := range astr {
if cnt[ch - 'a'] == 1 {
return false
}
cnt[ch - 'a'] ++
}
return true
}
func main() {
fmt.Println(isUnique("leetcode")) // false
fmt.Println(isUnique("abc")) // true
fmt.Println(isUnique1("leetcode")) // false
fmt.Println(isUnique1("abc")) // true
}