thảo luận Leetcode mỗi ngày

  • Người tạo chủ đề Người tạo chủ đề _Gia_Cat_Luong_
  • Ngày bắt đầu Ngày bắt đầu
Trạng thái
Không mở để trả lời thêm.
C-like:
impl Solution {
    fn set_bit_count(mut num: i32) -> i32 {
        let mut c = 0;
        while num != 0 {
            c += 1;
            num &= num - 1;
        }
        c
    }
    pub fn min_bit_flips(start: i32, goal: i32) -> i32 {
        Self::set_bit_count(start ^ goal)
    }
}
 
JavaScript:
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;
};
 
Good morning
Java:
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;
    }
}
 
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 :D
Tôi thích template này hơn: https://leetcode.com/discuss/genera...-Binary-Search-Template.-Solved-many-problems
Với đọc cái này: https://leetcode.com/problems/binar...arch-101-The-Ultimate-Binary-Search-Handbook/
 
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;
    }
}
 
C++:
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;
    }
};
 
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;
    }
}
Sao đoạn dưới xử lý cồng kềnh thế bác, sao không check luôn freqAllowed[c - 'a'].
 
Python:
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)
 
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 đó fence
4gmOAMB.png
 
Python:
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
 
JavaScript:
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;
};
3 ngày tới chắc 3 bài Hard :doubt:
 
Java:
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;
    }
}
 
Trạng thái
Không mở để trả lời thêm.

Thống kê chủ đề

Ngày tạo
_Gia_Cat_Luong_,
Người trả lời cuối
Vipluckystar,
Trả lời
17.755
Lượt xem
1.213.944
Quay lại
Lên đầu trang