Skip to content

Commit 40a0d2f

Browse files
committed
feat: add solutions to lc problem: No.1684
No.1684.Count the Number of Consistent Strings
1 parent 1faf9b5 commit 40a0d2f

File tree

5 files changed

+318
-0
lines changed

5 files changed

+318
-0
lines changed

solution/1600-1699/1684.Count the Number of Consistent Strings/README.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,137 @@ func countConsistentStrings(allowed string, words []string) (ans int) {
225225
}
226226
```
227227

228+
### **C**
229+
230+
```c
231+
int countConsistentStrings(char *allowed, char **words, int wordsSize) {
232+
int n = strlen(allowed);
233+
int make[26] = {0};
234+
for (int i = 0; i < n; i++) {
235+
make[allowed[i] - 'a'] = 1;
236+
}
237+
int ans = wordsSize;
238+
for (int i = 0; i < wordsSize; i++) {
239+
char *word = words[i];
240+
for (int j = 0; j < strlen(word); j++) {
241+
if (!make[word[j] - 'a']) {
242+
ans--;
243+
break;
244+
}
245+
}
246+
}
247+
return ans;
248+
}
249+
```
250+
251+
```c
252+
int helper(char *s) {
253+
int res = 0;
254+
int n = strlen(s);
255+
for (int i = 0; i < n; i++) {
256+
res |= 1 << (s[i] - 'a');
257+
}
258+
return res;
259+
}
260+
261+
int countConsistentStrings(char *allowed, char **words, int wordsSize) {
262+
int mask = helper(allowed);
263+
int ans = 0;
264+
for (int i = 0; i < wordsSize; i++) {
265+
if ((mask | helper(words[i])) == mask) {
266+
ans++;
267+
}
268+
}
269+
return ans;
270+
}
271+
```
272+
273+
### **TypeScript**
274+
275+
```ts
276+
function countConsistentStrings(allowed: string, words: string[]): number {
277+
const set = new Set([...allowed]);
278+
const n = words.length;
279+
let ans = n;
280+
for (const word of words) {
281+
for (const c of word) {
282+
if (!set.has(c)) {
283+
ans--;
284+
break;
285+
}
286+
}
287+
}
288+
return ans;
289+
}
290+
```
291+
292+
```ts
293+
function countConsistentStrings(allowed: string, words: string[]): number {
294+
const helper = (s: string) => {
295+
let res = 0;
296+
for (const c of s) {
297+
res |= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
298+
}
299+
return res;
300+
};
301+
const mask = helper(allowed);
302+
let ans = 0;
303+
for (const word of words) {
304+
if ((mask | helper(word)) === mask) {
305+
ans++;
306+
}
307+
}
308+
return ans;
309+
}
310+
```
311+
312+
### **Rust**
313+
314+
```rust
315+
impl Solution {
316+
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
317+
let n = words.len();
318+
let mut make = [false; 26];
319+
for c in allowed.as_bytes() {
320+
make[(c - b'a') as usize] = true;
321+
}
322+
let mut ans = n as i32;
323+
for word in words.iter() {
324+
for c in word.as_bytes().iter() {
325+
if !make[(c - b'a') as usize] {
326+
ans -= 1;
327+
break;
328+
}
329+
}
330+
}
331+
ans
332+
}
333+
}
334+
```
335+
336+
```rust
337+
impl Solution {
338+
fn helper(s: &String) -> i32 {
339+
let mut res = 0;
340+
for c in s.as_bytes().iter() {
341+
res |= 1 << (c - b'a') as i32;
342+
}
343+
res
344+
}
345+
346+
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
347+
let mask = Self::helper(&allowed);
348+
let mut ans = 0;
349+
for word in words.iter() {
350+
if (mask | Self::helper(word)) == mask {
351+
ans += 1;
352+
}
353+
}
354+
ans
355+
}
356+
}
357+
```
358+
228359
### **...**
229360

230361
```

solution/1600-1699/1684.Count the Number of Consistent Strings/README_EN.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,137 @@ func countConsistentStrings(allowed string, words []string) (ans int) {
199199
}
200200
```
201201

202+
### **C**
203+
204+
```c
205+
int countConsistentStrings(char *allowed, char **words, int wordsSize) {
206+
int n = strlen(allowed);
207+
int make[26] = {0};
208+
for (int i = 0; i < n; i++) {
209+
make[allowed[i] - 'a'] = 1;
210+
}
211+
int ans = wordsSize;
212+
for (int i = 0; i < wordsSize; i++) {
213+
char *word = words[i];
214+
for (int j = 0; j < strlen(word); j++) {
215+
if (!make[word[j] - 'a']) {
216+
ans--;
217+
break;
218+
}
219+
}
220+
}
221+
return ans;
222+
}
223+
```
224+
225+
```c
226+
int helper(char *s) {
227+
int res = 0;
228+
int n = strlen(s);
229+
for (int i = 0; i < n; i++) {
230+
res |= 1 << (s[i] - 'a');
231+
}
232+
return res;
233+
}
234+
235+
int countConsistentStrings(char *allowed, char **words, int wordsSize) {
236+
int mask = helper(allowed);
237+
int ans = 0;
238+
for (int i = 0; i < wordsSize; i++) {
239+
if ((mask | helper(words[i])) == mask) {
240+
ans++;
241+
}
242+
}
243+
return ans;
244+
}
245+
```
246+
247+
### **TypeScript**
248+
249+
```ts
250+
function countConsistentStrings(allowed: string, words: string[]): number {
251+
const set = new Set([...allowed]);
252+
const n = words.length;
253+
let ans = n;
254+
for (const word of words) {
255+
for (const c of word) {
256+
if (!set.has(c)) {
257+
ans--;
258+
break;
259+
}
260+
}
261+
}
262+
return ans;
263+
}
264+
```
265+
266+
```ts
267+
function countConsistentStrings(allowed: string, words: string[]): number {
268+
const helper = (s: string) => {
269+
let res = 0;
270+
for (const c of s) {
271+
res |= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
272+
}
273+
return res;
274+
};
275+
const mask = helper(allowed);
276+
let ans = 0;
277+
for (const word of words) {
278+
if ((mask | helper(word)) === mask) {
279+
ans++;
280+
}
281+
}
282+
return ans;
283+
}
284+
```
285+
286+
### **Rust**
287+
288+
```rust
289+
impl Solution {
290+
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
291+
let n = words.len();
292+
let mut make = [false; 26];
293+
for c in allowed.as_bytes() {
294+
make[(c - b'a') as usize] = true;
295+
}
296+
let mut ans = n as i32;
297+
for word in words.iter() {
298+
for c in word.as_bytes().iter() {
299+
if !make[(c - b'a') as usize] {
300+
ans -= 1;
301+
break;
302+
}
303+
}
304+
}
305+
ans
306+
}
307+
}
308+
```
309+
310+
```rust
311+
impl Solution {
312+
fn helper(s: &String) -> i32 {
313+
let mut res = 0;
314+
for c in s.as_bytes().iter() {
315+
res |= 1 << (c - b'a') as i32;
316+
}
317+
res
318+
}
319+
320+
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
321+
let mask = Self::helper(&allowed);
322+
let mut ans = 0;
323+
for word in words.iter() {
324+
if (mask | Self::helper(word)) == mask {
325+
ans += 1;
326+
}
327+
}
328+
ans
329+
}
330+
}
331+
```
332+
202333
### **...**
203334

204335
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
int helper(char *s) {
2+
int res = 0;
3+
int n = strlen(s);
4+
for (int i = 0; i < n; i++) {
5+
res |= 1 << (s[i] - 'a');
6+
}
7+
return res;
8+
}
9+
10+
int countConsistentStrings(char *allowed, char **words, int wordsSize) {
11+
int mask = helper(allowed);
12+
int ans = 0;
13+
for (int i = 0; i < wordsSize; i++) {
14+
if ((mask | helper(words[i])) == mask) {
15+
ans++;
16+
}
17+
}
18+
return ans;
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
impl Solution {
2+
fn helper(s: &String) -> i32 {
3+
let mut res = 0;
4+
for c in s.as_bytes().iter() {
5+
res |= 1 << (c - b'a') as i32;
6+
}
7+
res
8+
}
9+
10+
pub fn count_consistent_strings(allowed: String, words: Vec<String>) -> i32 {
11+
let mask = Self::helper(&allowed);
12+
let mut ans = 0;
13+
for word in words.iter() {
14+
if (mask | Self::helper(word)) == mask {
15+
ans += 1;
16+
}
17+
}
18+
ans
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function countConsistentStrings(allowed: string, words: string[]): number {
2+
const helper = (s: string) => {
3+
let res = 0;
4+
for (const c of s) {
5+
res |= 1 << (c.charCodeAt(0) - 'a'.charCodeAt(0));
6+
}
7+
return res;
8+
};
9+
const mask = helper(allowed);
10+
let ans = 0;
11+
for (const word of words) {
12+
if ((mask | helper(word)) === mask) {
13+
ans++;
14+
}
15+
}
16+
return ans;
17+
}

0 commit comments

Comments
 (0)