Skip to content

Commit bd9d5bc

Browse files
committed
feat: add solutions to lc problem: No.0953
No.0953.Verifying an Alien Dictionary
1 parent b09da00 commit bd9d5bc

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

solution/0900-0999/0953.Verifying an Alien Dictionary/README.md

+65
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,37 @@ func isAlienSorted(words []string, order string) bool {
165165
}
166166
```
167167

168+
### **TypeScript**
169+
170+
```ts
171+
function isAlienSorted(words: string[], order: string): boolean {
172+
const map = new Map();
173+
for (const c of order) {
174+
map.set(c, map.size);
175+
}
176+
const n = words.length;
177+
for (let i = 1; i < n; i++) {
178+
const s1 = words[i - 1];
179+
const s2 = words[i];
180+
const m = Math.min(s1.length, s2.length);
181+
let isEqual = false;
182+
for (let j = 0; j < m; j++) {
183+
if (map.get(s1[j]) > map.get(s2[j])) {
184+
return false;
185+
}
186+
if (map.get(s1[j]) < map.get(s2[j])) {
187+
isEqual = true;
188+
break;
189+
}
190+
}
191+
if (!isEqual && s1.length > s2.length) {
192+
return false;
193+
}
194+
}
195+
return true;
196+
}
197+
```
198+
168199
### **Rust**
169200

170201
```rust
@@ -198,6 +229,40 @@ impl Solution {
198229
}
199230
```
200231

232+
### **C**
233+
234+
```c
235+
#define min(a, b) (((a) < (b)) ? (a) : (b))
236+
237+
bool isAlienSorted(char **words, int wordsSize, char *order) {
238+
int map[26] = {0};
239+
for (int i = 0; i < 26; i++) {
240+
map[order[i] - 'a'] = i;
241+
}
242+
for (int i = 1; i < wordsSize; i++) {
243+
char *s1 = words[i - 1];
244+
char *s2 = words[i];
245+
int n = strlen(s1);
246+
int m = strlen(s2);
247+
int len = min(n, m);
248+
int isEqual = 1;
249+
for (int j = 0; j < len; j++) {
250+
if (map[s1[j] - 'a'] > map[s2[j] - 'a']) {
251+
return 0;
252+
}
253+
if (map[s1[j] - 'a'] < map[s2[j] - 'a']) {
254+
isEqual = 0;
255+
break;
256+
}
257+
}
258+
if (isEqual && n > m) {
259+
return 0;
260+
}
261+
}
262+
return 1;
263+
}
264+
```
265+
201266
### **...**
202267
203268
```

solution/0900-0999/0953.Verifying an Alien Dictionary/README_EN.md

+65
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,37 @@ func isAlienSorted(words []string, order string) bool {
155155
}
156156
```
157157

158+
### **TypeScript**
159+
160+
```ts
161+
function isAlienSorted(words: string[], order: string): boolean {
162+
const map = new Map();
163+
for (const c of order) {
164+
map.set(c, map.size);
165+
}
166+
const n = words.length;
167+
for (let i = 1; i < n; i++) {
168+
const s1 = words[i - 1];
169+
const s2 = words[i];
170+
const m = Math.min(s1.length, s2.length);
171+
let isEqual = false;
172+
for (let j = 0; j < m; j++) {
173+
if (map.get(s1[j]) > map.get(s2[j])) {
174+
return false;
175+
}
176+
if (map.get(s1[j]) < map.get(s2[j])) {
177+
isEqual = true;
178+
break;
179+
}
180+
}
181+
if (!isEqual && s1.length > s2.length) {
182+
return false;
183+
}
184+
}
185+
return true;
186+
}
187+
```
188+
158189
### **Rust**
159190

160191
```rust
@@ -188,6 +219,40 @@ impl Solution {
188219
}
189220
```
190221

222+
### **C**
223+
224+
```c
225+
#define min(a, b) (((a) < (b)) ? (a) : (b))
226+
227+
bool isAlienSorted(char **words, int wordsSize, char *order) {
228+
int map[26] = {0};
229+
for (int i = 0; i < 26; i++) {
230+
map[order[i] - 'a'] = i;
231+
}
232+
for (int i = 1; i < wordsSize; i++) {
233+
char *s1 = words[i - 1];
234+
char *s2 = words[i];
235+
int n = strlen(s1);
236+
int m = strlen(s2);
237+
int len = min(n, m);
238+
int isEqual = 1;
239+
for (int j = 0; j < len; j++) {
240+
if (map[s1[j] - 'a'] > map[s2[j] - 'a']) {
241+
return 0;
242+
}
243+
if (map[s1[j] - 'a'] < map[s2[j] - 'a']) {
244+
isEqual = 0;
245+
break;
246+
}
247+
}
248+
if (isEqual && n > m) {
249+
return 0;
250+
}
251+
}
252+
return 1;
253+
}
254+
```
255+
191256
### **...**
192257
193258
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#define min(a, b) (((a) < (b)) ? (a) : (b))
2+
3+
bool isAlienSorted(char **words, int wordsSize, char *order) {
4+
int map[26] = {0};
5+
for (int i = 0; i < 26; i++) {
6+
map[order[i] - 'a'] = i;
7+
}
8+
for (int i = 1; i < wordsSize; i++) {
9+
char *s1 = words[i - 1];
10+
char *s2 = words[i];
11+
int n = strlen(s1);
12+
int m = strlen(s2);
13+
int len = min(n, m);
14+
int isEqual = 1;
15+
for (int j = 0; j < len; j++) {
16+
if (map[s1[j] - 'a'] > map[s2[j] - 'a']) {
17+
return 0;
18+
}
19+
if (map[s1[j] - 'a'] < map[s2[j] - 'a']) {
20+
isEqual = 0;
21+
break;
22+
}
23+
}
24+
if (isEqual && n > m) {
25+
return 0;
26+
}
27+
}
28+
return 1;
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function isAlienSorted(words: string[], order: string): boolean {
2+
const map = new Map();
3+
for (const c of order) {
4+
map.set(c, map.size);
5+
}
6+
const n = words.length;
7+
for (let i = 1; i < n; i++) {
8+
const s1 = words[i - 1];
9+
const s2 = words[i];
10+
const m = Math.min(s1.length, s2.length);
11+
let isEqual = false;
12+
for (let j = 0; j < m; j++) {
13+
if (map.get(s1[j]) > map.get(s2[j])) {
14+
return false;
15+
}
16+
if (map.get(s1[j]) < map.get(s2[j])) {
17+
isEqual = true;
18+
break;
19+
}
20+
}
21+
if (!isEqual && s1.length > s2.length) {
22+
return false;
23+
}
24+
}
25+
return true;
26+
}

0 commit comments

Comments
 (0)