File tree Expand file tree Collapse file tree 6 files changed +246
-4
lines changed
solution/1600-1699/1684.Count the Number of Consistent Strings Expand file tree Collapse file tree 6 files changed +246
-4
lines changed Original file line number Diff line number Diff line change 4848 <li><code>words[i]</code> 和 <code>allowed</code> 只包含小写英文字母。</li>
4949</ul >
5050
51-
5251## 解法
5352
5453<!-- 这里可写通用的实现逻辑 -->
6059<!-- 这里可写当前语言的特殊实现逻辑 -->
6160
6261``` python
63-
62+ class Solution :
63+ def countConsistentStrings (self , allowed : str , words : List[str ]) -> int :
64+ res = 0
65+ chars = set (allowed)
66+ for word in words:
67+ find = True
68+ for c in word:
69+ if c not in chars:
70+ find = False
71+ break
72+ if find:
73+ res += 1
74+ return res
6475```
6576
6677### ** Java**
6778
6879<!-- 这里可写当前语言的特殊实现逻辑 -->
6980
7081``` java
82+ class Solution {
83+ public int countConsistentStrings (String allowed , String [] words ) {
84+ boolean [] chars = new boolean [26 ];
85+ for (char c : allowed. toCharArray()) {
86+ chars[c - ' a' ] = true ;
87+ }
88+ int res = 0 ;
89+ for (String word : words) {
90+ boolean find = true ;
91+ for (char c : word. toCharArray()) {
92+ if (! chars[c - ' a' ]) {
93+ find = false ;
94+ break ;
95+ }
96+ }
97+ if (find) {
98+ ++ res;
99+ }
100+ }
101+ return res;
102+ }
103+ }
104+ ```
105+
106+ ### ** C++**
107+
108+ ``` cpp
109+ class Solution {
110+ public:
111+ int countConsistentStrings(string allowed, vector<string >& words) {
112+ vector<bool > chars(26, false);
113+ for (char c : allowed) {
114+ chars[ c - 'a'] = true;
115+ }
116+ int res = 0;
117+ for (string word : words) {
118+ bool find = true;
119+ for (char c : word) {
120+ if (!chars[ c - 'a'] ) {
121+ find = false;
122+ break;
123+ }
124+ }
125+ if (find) ++res;
126+ }
127+ return res;
128+ }
129+ };
130+ ```
71131
132+ ### **Go**
133+
134+ ```go
135+ func countConsistentStrings(allowed string, words []string) int {
136+ chars := [26]bool{}
137+ for _, c := range allowed {
138+ chars[c-'a'] = true
139+ }
140+ res := 0
141+ for _, word := range words {
142+ find := true
143+ for _, c := range word {
144+ if !chars[c-'a'] {
145+ find = false
146+ break
147+ }
148+ }
149+ if find {
150+ res++
151+ }
152+ }
153+ return res
154+ }
72155```
73156
74157### ** ...**
Original file line number Diff line number Diff line change 4444 <li><code>words[i]</code> and <code>allowed</code> contain only lowercase English letters.</li>
4545</ul >
4646
47-
4847## Solutions
4948
5049<!-- tabs:start -->
5150
5251### ** Python3**
5352
5453``` python
55-
54+ class Solution :
55+ def countConsistentStrings (self , allowed : str , words : List[str ]) -> int :
56+ res = 0
57+ chars = set (allowed)
58+ for word in words:
59+ find = True
60+ for c in word:
61+ if c not in chars:
62+ find = False
63+ break
64+ if find:
65+ res += 1
66+ return res
5667```
5768
5869### ** Java**
5970
6071``` java
72+ class Solution {
73+ public int countConsistentStrings (String allowed , String [] words ) {
74+ boolean [] chars = new boolean [26 ];
75+ for (char c : allowed. toCharArray()) {
76+ chars[c - ' a' ] = true ;
77+ }
78+ int res = 0 ;
79+ for (String word : words) {
80+ boolean find = true ;
81+ for (char c : word. toCharArray()) {
82+ if (! chars[c - ' a' ]) {
83+ find = false ;
84+ break ;
85+ }
86+ }
87+ if (find) {
88+ ++ res;
89+ }
90+ }
91+ return res;
92+ }
93+ }
94+ ```
95+
96+ ### ** C++**
97+
98+ ``` cpp
99+ class Solution {
100+ public:
101+ int countConsistentStrings(string allowed, vector<string >& words) {
102+ vector<bool > chars(26, false);
103+ for (char c : allowed) {
104+ chars[ c - 'a'] = true;
105+ }
106+ int res = 0;
107+ for (string word : words) {
108+ bool find = true;
109+ for (char c : word) {
110+ if (!chars[ c - 'a'] ) {
111+ find = false;
112+ break;
113+ }
114+ }
115+ if (find) ++res;
116+ }
117+ return res;
118+ }
119+ };
120+ ```
61121
122+ ### **Go**
123+
124+ ```go
125+ func countConsistentStrings(allowed string, words []string) int {
126+ chars := [26]bool{}
127+ for _, c := range allowed {
128+ chars[c-'a'] = true
129+ }
130+ res := 0
131+ for _, word := range words {
132+ find := true
133+ for _, c := range word {
134+ if !chars[c-'a'] {
135+ find = false
136+ break
137+ }
138+ }
139+ if find {
140+ res++
141+ }
142+ }
143+ return res
144+ }
62145```
63146
64147### ** ...**
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public:
3+ int countConsistentStrings (string allowed, vector<string>& words) {
4+ vector<bool > chars (26 , false );
5+ for (char c : allowed) {
6+ chars[c - ' a' ] = true ;
7+ }
8+ int res = 0 ;
9+ for (string word : words) {
10+ bool find = true ;
11+ for (char c : word) {
12+ if (!chars[c - ' a' ]) {
13+ find = false ;
14+ break ;
15+ }
16+ }
17+ if (find) ++res;
18+ }
19+ return res;
20+ }
21+ };
Original file line number Diff line number Diff line change 1+ func countConsistentStrings (allowed string , words []string ) int {
2+ chars := [26 ]bool {}
3+ for _ , c := range allowed {
4+ chars [c - 'a' ] = true
5+ }
6+ res := 0
7+ for _ , word := range words {
8+ find := true
9+ for _ , c := range word {
10+ if ! chars [c - 'a' ] {
11+ find = false
12+ break
13+ }
14+ }
15+ if find {
16+ res ++
17+ }
18+ }
19+ return res
20+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ public int countConsistentStrings (String allowed , String [] words ) {
3+ boolean [] chars = new boolean [26 ];
4+ for (char c : allowed .toCharArray ()) {
5+ chars [c - 'a' ] = true ;
6+ }
7+ int res = 0 ;
8+ for (String word : words ) {
9+ boolean find = true ;
10+ for (char c : word .toCharArray ()) {
11+ if (!chars [c - 'a' ]) {
12+ find = false ;
13+ break ;
14+ }
15+ }
16+ if (find ) {
17+ ++res ;
18+ }
19+ }
20+ return res ;
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ class Solution :
2+ def countConsistentStrings (self , allowed : str , words : List [str ]) -> int :
3+ res = 0
4+ chars = set (allowed )
5+ for word in words :
6+ find = True
7+ for c in word :
8+ if c not in chars :
9+ find = False
10+ break
11+ if find :
12+ res += 1
13+ return res
You can’t perform that action at this time.
0 commit comments