Skip to content

Commit c184b8e

Browse files
committedJul 15, 2022
feat: add solutions to lc problem: No.1698
No.1698.Number of Distinct Substrings in a String
1 parent 730f6ab commit c184b8e

File tree

6 files changed

+144
-2
lines changed

6 files changed

+144
-2
lines changed
 

‎solution/1600-1699/1698.Number of Distinct Substrings in a String/README.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,65 @@
4949
<!-- 这里可写当前语言的特殊实现逻辑 -->
5050

5151
```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)})
5356
```
5457

5558
### **Java**
5659

5760
<!-- 这里可写当前语言的特殊实现逻辑 -->
5861

5962
```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+
```
6098
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+
}
61111
```
62112

63113
### **...**

‎solution/1600-1699/1698.Number of Distinct Substrings in a String/README_EN.md

+51-1
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,63 @@
4242
### **Python3**
4343

4444
```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)})
4649
```
4750

4851
### **Java**
4952

5053
```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+
```
5189
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+
}
52102
```
53103

54104
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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)})

0 commit comments

Comments
 (0)
Please sign in to comment.