freedom.9
Senior Member
Fence ở Can tầm 1 năm nữa thì cũng sẽ bị tư bản nó bào như toy ko có tiền mua Premium

Fence ở Can tầm 1 năm nữa thì cũng sẽ bị tư bản nó bào như toy ko có tiền mua Premium

Vẫn đang ở VN fen à, giữa tháng sau mới bayFence ở Can tầm 1 năm nữa thì cũng sẽ bị tư bản nó bào như toy ko có tiền mua Premium![]()



var countConsistentStrings = function(allowed, words) {
const isConsistent = (word, set) => {
for (const c of word) {
if (!set.has(c)) {
return false;
}
}
return true;
}
const set = new Set(allowed);
return words.filter(word => isConsistent(word, set)).length;
};
class Solution {
public int countConsistentStrings(String allowed, String[] words) {
int[] counts = new int[26];
for (int i = 0; i < allowed.length(); i++) {
char ch = allowed.charAt(i);
counts[ch - 'a'] = 1;
}
int res = 0;
for (String word : words) {
int i;
for (i = 0; i < word.length(); i++) {
char ch = word.charAt(i);
if (counts[ch - 'a'] == 0) {
break;
}
}
res += i == word.length() ? 1 : 0;
}
return res;
}
}
Tôi thích template này hơn: https://leetcode.com/discuss/genera...-Binary-Search-Template.-Solved-many-problemsMình có đọc được 1 ý của fen @freedom.9 là binary search chỉ cần đúng cái template 1: https://leetcode.com/explore/learn/card/binary-search/125/template-i/938/ là chiến hết tất cả dạng bài. Tin này còn chuẩn ko fen, dạo này bị BS hành nên lội lại đọc xem ý kiến của các cao nhân![]()
Chuẩn mà fence, thêm 1 biến để gán kết quả nữa là xong. Tin siêu chuẩn nhéMình có đọc được 1 ý của fen @freedom.9 là binary search chỉ cần đúng cái template 1: https://leetcode.com/explore/learn/card/binary-search/125/template-i/938/ là chiến hết tất cả dạng bài. Tin này còn chuẩn ko fen, dạo này bị BS hành nên lội lại đọc xem ý kiến của các cao nhân![]()

Em cũng đang dùng cái template này, nhưng mà là do soi sol thấy hay nên học thuộc luônMình có đọc được 1 ý của fen @freedom.9 là binary search chỉ cần đúng cái template 1: https://leetcode.com/explore/learn/card/binary-search/125/template-i/938/ là chiến hết tất cả dạng bài. Tin này còn chuẩn ko fen, dạo này bị BS hành nên lội lại đọc xem ý kiến của các cao nhân![]()
.class Solution {
public int countConsistentStrings(String allowed, String[] words) {
int ans = 0;
int[] freqAllowed = new int[26];
for (char c : allowed.toCharArray()) {
freqAllowed[c - 'a']++;
}
for (String word : words) {
int[] freq = new int[26];
for (char c : word.toCharArray()) {
freq[c - 'a']++;
}
ans++;
for (int i = 0; i < 26; i++) {
if (freqAllowed[i] == 0 && freq[i] != 0) {
ans--;
break;
}
}
}
return ans;
}
}
class Solution {
public:
int countConsistentStrings(string allowed, vector<string>& words) {
vector<int> count(26, 0);
for (int i = 0; i < allowed.length(); i++) {
count[allowed[i] - 'a']++;
}
int ans = 0;
for (int i = 0; i < words.size(); i++) {
int isConsisted = true;
for (int j = 0; j < words[i].length(); j++) {
if (count[words[i][j] - 'a'] == 0) {
isConsisted = false;
break;
}
}
if (isConsisted) ans++;
}
return ans;
}
};
Sao đoạn dưới xử lý cồng kềnh thế bác, sao không check luôn freqAllowed[c - 'a'].Java:class Solution { public int countConsistentStrings(String allowed, String[] words) { int ans = 0; int[] freqAllowed = new int[26]; for (char c : allowed.toCharArray()) { freqAllowed[c - 'a']++; } for (String word : words) { int[] freq = new int[26]; for (char c : word.toCharArray()) { freq[c - 'a']++; } ans++; for (int i = 0; i < 26; i++) { if (freqAllowed[i] == 0 && freq[i] != 0) { ans--; break; } } } return ans; } }
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
return sum(all(c in set(allowed) for c in word) for word in words)
Không thấy đội cảnh sát vào ghi biên bản trường hợp này nhỉ?Sao đoạn dưới xử lý cồng kềnh thế bác, sao không check luôn freqAllowed[c - 'a'].

Người ta gọi đó là giới hạn DNA đó fenceSao đoạn dưới xử lý cồng kềnh thế bác, sao không check luôn freqAllowed[c - 'a'].
class Solution:
def countConsistentStrings(self, allowed: str, words: List[str]) -> int:
allowedSet = set(allowed)
def isAllowed(s):
for c in s:
if not c in allowedSet:
return False
return True
result = 0
for word in words:
if isAllowed(word):
result += 1
return result
function countConsistentStrings(allowed: string, words: string[]): number {
const arr = new Array(26).fill(0);
for (const c of allowed) arr[c.charCodeAt(0) - 'a'.charCodeAt(0)]++
let res = 0;
for (const w of words) {
let check = true;
for (const c of w) {
if (!arr[c.charCodeAt(0) - 'a'.charCodeAt(0)]) {
check = false;
break;
}
}
res += check ? 1 : 0;
}
return res;
};

Em lâu rồi không lội page thread này nên không rõ mình bị dinh tội gì thế bácKhông thấy đội cảnh sát vào ghi biên bản trường hợp này nhỉ?![]()
.class Solution {
public int countConsistentStrings(String allowed, String[] words) {
int[] map = new int[135];
int count = 0;
for(int i = 0; i < allowed.length(); i++) {
map[allowed.charAt(i)]++;
}
for1: for(String s : words) {
for(int i = 0; i < s.length(); i++) {
if (map[s.charAt(i)] == 0) {
continue for1;
}
}
count++;
}
return count;
}
}
làm hard riết mất não chán nản quá 