Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 65b12f5

Browse files
feat: add solutions to lc problem: No.2306 (doocs#1992)
No.2306.Naming a Company
1 parent fdf40b4 commit 65b12f5

File tree

5 files changed

+147
-54
lines changed

5 files changed

+147
-54
lines changed

‎solution/2300-2399/2306.Naming a Company/README.md‎

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,15 @@
6262

6363
**方法一:枚举计数**
6464

65-
$f[i][j]$ 表示有多少个首字母为 $i$ 的字符串,将首字符换成 $j$ 后,未在 $ideas$ 中出现过的字符串个数。
65+
我们定义 $f[i][j]$ 表示 $ideas$ 中以第 $i$ 个字母开头,替换为第 $j$ 个字母后,不在 $ideas$ 中的字符串的个数。初始时 $f[i][j] = 0$。另外,用一个哈希表 $s$ 记录 $ideas$ 中的字符串,方便我们开快速判断某个字符串是否在 $ideas$ 中。
66+
67+
接下来,我们遍历 $ideas$ 中字符串,对于当前遍历到的字符串 $v,ドル我们枚举替换后的第一个字母 $j,ドル如果 $v$ 替换后的字符串不在 $ideas$ 中,那么我们就更新 $f[i][j] = f[i][j] + 1$。
68+
69+
最后,我们再次遍历 $ideas$ 中字符串,对于当前遍历到的字符串 $v,ドル我们枚举替换后的第一个字母 $j,ドル如果 $v$ 替换后的字符串不在 $ideas$ 中,那么我们就更新答案 $ans = ans + f[j][i]$。
70+
71+
最终答案即为 $ans$。
72+
73+
时间复杂度 $O(n \times m \times |\Sigma|),ドル空间复杂度 $O(|\Sigma|^2)$。其中 $n$ 和 $m$ 分别是 $ideas$ 中字符串的个数和字符串的最大长度,而 $|\Sigma|$ 是字符串中出现的字符集,本题中 $|\Sigma| \leq 26$。
6674

6775
<!-- tabs:start -->
6876

@@ -138,7 +146,7 @@ class Solution {
138146
public:
139147
long long distinctNames(vector<string>& ideas) {
140148
unordered_set<string> s(ideas.begin(), ideas.end());
141-
vector<vector<int>> f(26, vector<int>(26));
149+
int f[26][26]{};
142150
for (auto v : ideas) {
143151
int i = v[0] - 'a';
144152
for (int j = 0; j < 26; ++j) {
@@ -149,7 +157,7 @@ public:
149157
}
150158
}
151159
long long ans = 0;
152-
for (auto v : ideas) {
160+
for (auto& v : ideas) {
153161
int i = v[0] - 'a';
154162
for (int j = 0; j < 26; ++j) {
155163
v[0] = j + 'a';
@@ -166,15 +174,12 @@ public:
166174
### **Go**
167175
168176
```go
169-
func distinctNames(ideas []string) int64 {
177+
func distinctNames(ideas []string) (ans int64) {
170178
s := map[string]bool{}
171179
for _, v := range ideas {
172180
s[v] = true
173181
}
174-
f := make([][]int, 26)
175-
for i := range f {
176-
f[i] = make([]int, 26)
177-
}
182+
f := [26][26]int{}
178183
for _, v := range ideas {
179184
i := int(v[0] - 'a')
180185
t := []byte(v)
@@ -185,7 +190,7 @@ func distinctNames(ideas []string) int64 {
185190
}
186191
}
187192
}
188-
var ans int64
193+
189194
for _, v := range ideas {
190195
i := int(v[0] - 'a')
191196
t := []byte(v)
@@ -196,14 +201,41 @@ func distinctNames(ideas []string) int64 {
196201
}
197202
}
198203
}
199-
return ans
204+
return
200205
}
201206
```
202207

203208
### **TypeScript**
204209

205210
```ts
206-
211+
function distinctNames(ideas: string[]): number {
212+
const s = new Set(ideas);
213+
const f: number[][] = Array(26)
214+
.fill(0)
215+
.map(() => Array(26).fill(0));
216+
for (const v of s) {
217+
const i = v.charCodeAt(0) - 'a'.charCodeAt(0);
218+
const t = [...v];
219+
for (let j = 0; j < 26; ++j) {
220+
t[0] = String.fromCharCode('a'.charCodeAt(0) + j);
221+
if (!s.has(t.join(''))) {
222+
f[i][j]++;
223+
}
224+
}
225+
}
226+
let ans = 0;
227+
for (const v of s) {
228+
const i = v.charCodeAt(0) - 'a'.charCodeAt(0);
229+
const t = [...v];
230+
for (let j = 0; j < 26; ++j) {
231+
t[0] = String.fromCharCode('a'.charCodeAt(0) + j);
232+
if (!s.has(t.join(''))) {
233+
ans += f[j][i];
234+
}
235+
}
236+
}
237+
return ans;
238+
}
207239
```
208240

209241
### **...**

‎solution/2300-2399/2306.Naming a Company/README_EN.md‎

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@ The following are some examples of invalid selections:
5656

5757
## Solutions
5858

59+
**Solution 1: Enumeration Counting**
60+
61+
We define $f[i][j]$ to represent the number of strings in $ideas$ that start with the $i$th letter and are not in $ideas$ after being replaced with the $j$th letter. Initially, $f[i][j] = 0$. Additionally, we use a hash table $s$ to record the strings in $ideas,ドル which allows us to quickly determine whether a string is in $ideas$.
62+
63+
Next, we traverse the strings in $ideas$. For the current string $v,ドル we enumerate the first letter $j$ after replacement. If the string after $v$ is replaced is not in $ideas,ドル then we update $f[i][j] = f[i][j] + 1$.
64+
65+
Finally, we traverse the strings in $ideas$ again. For the current string $v,ドル we enumerate the first letter $j$ after replacement. If the string after $v$ is replaced is not in $ideas,ドル then we update the answer $ans = ans + f[j][i]$.
66+
67+
The final answer is $ans$.
68+
69+
The time complexity is $O(n \times m \times |\Sigma|),ドル and the space complexity is $O(|\Sigma|^2)$. Here, $n$ and $m$ are the number of strings in $ideas$ and the maximum length of the strings, respectively, and $|\Sigma|$ is the character set that appears in the string. In this problem, $|\Sigma| \leq 26$.
70+
5971
<!-- tabs:start -->
6072

6173
### **Python3**
@@ -126,7 +138,7 @@ class Solution {
126138
public:
127139
long long distinctNames(vector<string>& ideas) {
128140
unordered_set<string> s(ideas.begin(), ideas.end());
129-
vector<vector<int>> f(26, vector<int>(26));
141+
int f[26][26]{};
130142
for (auto v : ideas) {
131143
int i = v[0] - 'a';
132144
for (int j = 0; j < 26; ++j) {
@@ -137,7 +149,7 @@ public:
137149
}
138150
}
139151
long long ans = 0;
140-
for (auto v : ideas) {
152+
for (auto& v : ideas) {
141153
int i = v[0] - 'a';
142154
for (int j = 0; j < 26; ++j) {
143155
v[0] = j + 'a';
@@ -154,15 +166,12 @@ public:
154166
### **Go**
155167
156168
```go
157-
func distinctNames(ideas []string) int64 {
169+
func distinctNames(ideas []string) (ans int64) {
158170
s := map[string]bool{}
159171
for _, v := range ideas {
160172
s[v] = true
161173
}
162-
f := make([][]int, 26)
163-
for i := range f {
164-
f[i] = make([]int, 26)
165-
}
174+
f := [26][26]int{}
166175
for _, v := range ideas {
167176
i := int(v[0] - 'a')
168177
t := []byte(v)
@@ -173,7 +182,7 @@ func distinctNames(ideas []string) int64 {
173182
}
174183
}
175184
}
176-
var ans int64
185+
177186
for _, v := range ideas {
178187
i := int(v[0] - 'a')
179188
t := []byte(v)
@@ -184,14 +193,41 @@ func distinctNames(ideas []string) int64 {
184193
}
185194
}
186195
}
187-
return ans
196+
return
188197
}
189198
```
190199

191200
### **TypeScript**
192201

193202
```ts
194-
203+
function distinctNames(ideas: string[]): number {
204+
const s = new Set(ideas);
205+
const f: number[][] = Array(26)
206+
.fill(0)
207+
.map(() => Array(26).fill(0));
208+
for (const v of s) {
209+
const i = v.charCodeAt(0) - 'a'.charCodeAt(0);
210+
const t = [...v];
211+
for (let j = 0; j < 26; ++j) {
212+
t[0] = String.fromCharCode('a'.charCodeAt(0) + j);
213+
if (!s.has(t.join(''))) {
214+
f[i][j]++;
215+
}
216+
}
217+
}
218+
let ans = 0;
219+
for (const v of s) {
220+
const i = v.charCodeAt(0) - 'a'.charCodeAt(0);
221+
const t = [...v];
222+
for (let j = 0; j < 26; ++j) {
223+
t[0] = String.fromCharCode('a'.charCodeAt(0) + j);
224+
if (!s.has(t.join(''))) {
225+
ans += f[j][i];
226+
}
227+
}
228+
}
229+
return ans;
230+
}
195231
```
196232

197233
### **...**
Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
class Solution {
2-
public:
3-
long long distinctNames(vector<string>& ideas) {
4-
unordered_set<string> s(ideas.begin(), ideas.end());
5-
vector<vector<int>> f(26, vector<int>(26));
6-
for (auto v : ideas) {
7-
int i = v[0] - 'a';
8-
for (int j = 0; j < 26; ++j) {
9-
v[0] = j + 'a';
10-
if (!s.count(v)) {
11-
++f[i][j];
12-
}
13-
}
14-
}
15-
long long ans = 0;
16-
for (auto v : ideas) {
17-
int i = v[0] - 'a';
18-
for (int j = 0; j < 26; ++j) {
19-
v[0] = j + 'a';
20-
if (!s.count(v)) {
21-
ans += f[j][i];
22-
}
23-
}
24-
}
25-
return ans;
26-
}
1+
class Solution {
2+
public:
3+
long long distinctNames(vector<string>& ideas) {
4+
unordered_set<string> s(ideas.begin(), ideas.end());
5+
int f[26][26]{};
6+
for (auto v : ideas) {
7+
int i = v[0] - 'a';
8+
for (int j = 0; j < 26; ++j) {
9+
v[0] = j + 'a';
10+
if (!s.count(v)) {
11+
++f[i][j];
12+
}
13+
}
14+
}
15+
long long ans = 0;
16+
for (auto& v : ideas) {
17+
int i = v[0] - 'a';
18+
for (int j = 0; j < 26; ++j) {
19+
v[0] = j + 'a';
20+
if (!s.count(v)) {
21+
ans += f[j][i];
22+
}
23+
}
24+
}
25+
return ans;
26+
}
2727
};
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
1-
func distinctNames(ideas []string) int64 {
1+
func distinctNames(ideas []string) (ansint64) {
22
s := map[string]bool{}
33
for _, v := range ideas {
44
s[v] = true
55
}
6-
f := make([][]int, 26)
7-
for i := range f {
8-
f[i] = make([]int, 26)
9-
}
6+
f := [26][26]int{}
107
for _, v := range ideas {
118
i := int(v[0] - 'a')
129
t := []byte(v)
@@ -17,7 +14,7 @@ func distinctNames(ideas []string) int64 {
1714
}
1815
}
1916
}
20-
varansint64
17+
2118
for _, v := range ideas {
2219
i := int(v[0] - 'a')
2320
t := []byte(v)
@@ -28,5 +25,5 @@ func distinctNames(ideas []string) int64 {
2825
}
2926
}
3027
}
31-
returnans
28+
return
3229
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function distinctNames(ideas: string[]): number {
2+
const s = new Set(ideas);
3+
const f: number[][] = Array(26)
4+
.fill(0)
5+
.map(() => Array(26).fill(0));
6+
for (const v of s) {
7+
const i = v.charCodeAt(0) - 'a'.charCodeAt(0);
8+
const t = [...v];
9+
for (let j = 0; j < 26; ++j) {
10+
t[0] = String.fromCharCode('a'.charCodeAt(0) + j);
11+
if (!s.has(t.join(''))) {
12+
f[i][j]++;
13+
}
14+
}
15+
}
16+
let ans = 0;
17+
for (const v of s) {
18+
const i = v.charCodeAt(0) - 'a'.charCodeAt(0);
19+
const t = [...v];
20+
for (let j = 0; j < 26; ++j) {
21+
t[0] = String.fromCharCode('a'.charCodeAt(0) + j);
22+
if (!s.has(t.join(''))) {
23+
ans += f[j][i];
24+
}
25+
}
26+
}
27+
return ans;
28+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /