File tree 6 files changed +144
-2
lines changed
solution/1600-1699/1698.Number of Distinct Substrings in a String
6 files changed +144
-2
lines changed Original file line number Diff line number Diff line change 49
49
<!-- 这里可写当前语言的特殊实现逻辑 -->
50
50
51
51
``` python
52
-
52
+ class Solution :
53
+ def countDistinct (self , s : str ) -> int :
54
+ n = len (s)
55
+ return len ({s[i: j] for i in range (n) for j in range (i + 1 , n + 1 )})
53
56
```
54
57
55
58
### ** Java**
56
59
57
60
<!-- 这里可写当前语言的特殊实现逻辑 -->
58
61
59
62
``` java
63
+ class Solution {
64
+ public int countDistinct (String s ) {
65
+ Set<String > ss = new HashSet<> ();
66
+ int n = s. length();
67
+ for (int i = 0 ; i < n; ++ i) {
68
+ for (int j = i + 1 ; j <= n; ++ j) {
69
+ ss. add(s. substring(i, j));
70
+ }
71
+ }
72
+ return ss. size();
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### ** C++**
78
+
79
+ ``` cpp
80
+ class Solution {
81
+ public:
82
+ int countDistinct(string s) {
83
+ unordered_set<string_view> ss;
84
+ int n = s.size();
85
+ string_view t, v = s;
86
+ for (int i = 0; i < n; ++i)
87
+ {
88
+ for (int j = i + 1; j <= n; ++j)
89
+ {
90
+ t = v.substr(i, j - i);
91
+ ss.insert(t);
92
+ }
93
+ }
94
+ return ss.size();
95
+ }
96
+ };
97
+ ```
60
98
99
+ ### **Go**
100
+
101
+ ```go
102
+ func countDistinct(s string) int {
103
+ ss := map[string]bool{}
104
+ for i := range s {
105
+ for j := i + 1; j <= len(s); j++ {
106
+ ss[s[i:j]] = true
107
+ }
108
+ }
109
+ return len(ss)
110
+ }
61
111
```
62
112
63
113
### ** ...**
Original file line number Diff line number Diff line change 42
42
### ** Python3**
43
43
44
44
``` python
45
-
45
+ class Solution :
46
+ def countDistinct (self , s : str ) -> int :
47
+ n = len (s)
48
+ return len ({s[i: j] for i in range (n) for j in range (i + 1 , n + 1 )})
46
49
```
47
50
48
51
### ** Java**
49
52
50
53
``` java
54
+ class Solution {
55
+ public int countDistinct (String s ) {
56
+ Set<String > ss = new HashSet<> ();
57
+ int n = s. length();
58
+ for (int i = 0 ; i < n; ++ i) {
59
+ for (int j = i + 1 ; j <= n; ++ j) {
60
+ ss. add(s. substring(i, j));
61
+ }
62
+ }
63
+ return ss. size();
64
+ }
65
+ }
66
+ ```
67
+
68
+ ### ** C++**
69
+
70
+ ``` cpp
71
+ class Solution {
72
+ public:
73
+ int countDistinct(string s) {
74
+ unordered_set<string_view> ss;
75
+ int n = s.size();
76
+ string_view t, v = s;
77
+ for (int i = 0; i < n; ++i)
78
+ {
79
+ for (int j = i + 1; j <= n; ++j)
80
+ {
81
+ t = v.substr(i, j - i);
82
+ ss.insert(t);
83
+ }
84
+ }
85
+ return ss.size();
86
+ }
87
+ };
88
+ ```
51
89
90
+ ### **Go**
91
+
92
+ ```go
93
+ func countDistinct(s string) int {
94
+ ss := map[string]bool{}
95
+ for i := range s {
96
+ for j := i + 1; j <= len(s); j++ {
97
+ ss[s[i:j]] = true
98
+ }
99
+ }
100
+ return len(ss)
101
+ }
52
102
```
53
103
54
104
### ** ...**
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int countDistinct (string s) {
4
+ unordered_set<string_view> ss;
5
+ int n = s.size ();
6
+ string_view t, v = s;
7
+ for (int i = 0 ; i < n; ++i)
8
+ {
9
+ for (int j = i + 1 ; j <= n; ++j)
10
+ {
11
+ t = v.substr (i, j - i);
12
+ ss.insert (t);
13
+ }
14
+ }
15
+ return ss.size ();
16
+ }
17
+ };
Original file line number Diff line number Diff line change
1
+ func countDistinct (s string ) int {
2
+ ss := map [string ]bool {}
3
+ for i := range s {
4
+ for j := i + 1 ; j <= len (s ); j ++ {
5
+ ss [s [i :j ]] = true
6
+ }
7
+ }
8
+ return len (ss )
9
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public int countDistinct (String s ) {
3
+ Set <String > ss = new HashSet <>();
4
+ int n = s .length ();
5
+ for (int i = 0 ; i < n ; ++i ) {
6
+ for (int j = i + 1 ; j <= n ; ++j ) {
7
+ ss .add (s .substring (i , j ));
8
+ }
9
+ }
10
+ return ss .size ();
11
+ }
12
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def countDistinct (self , s : str ) -> int :
3
+ n = len (s )
4
+ return len ({s [i : j ] for i in range (n ) for j in range (i + 1 , n + 1 )})
You can’t perform that action at this time.
0 commit comments