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.
easy peasy
JavaScript:
function wordBreak(s: string, wordDict: string[]): boolean {
    const n = s.length, set = new Set(wordDict) ;
    let dp = Array(n+1).fill(false);
    dp[0] = true;
    for (let i = 1; i <= n; i++) {
        for (let j = 0; j < i; j++) {
            if (dp[j] && set.has(s.substring(j,i))) {
                dp[i] = true;
                break;
            }
        }
    }
    return dp[n];
};
 
easy peasy
JavaScript:
function wordBreak(s: string, wordDict: string[]): boolean {
    const n = s.length, set = new Set(wordDict) ;
    let dp = Array(n+1).fill(false);
    dp[0] = true;
    for (let i = 1; i <= n; i++) {
        for (let j = 0; j < i; j++) {
            if (dp[j] && set.has(s.substring(j,i))) {
                dp[i] = true;
                break;
            }
        }
    }
    return dp[n];
};
O(n^3) :shame:
 
C#:
public class Solution {
    public bool WordBreak(string s, IList<string> wordDict) {
        var dp = new bool[s.Length];
        for(int i = 0; i< dp.Length; i++)
        {
            foreach(var item in wordDict)
            {
                if(i  < item.Length - 1)
                    continue;

                if(i == item.Length - 1 || dp[i - item.Length])
                {
                    if (s.Substring(i - item.Length + 1, item.Length).Equals(item)) {
                        dp[i] = true;   
                        break;
                    }
                }
            }
        }

        return dp[s.Length - 1];
    }
}
 
Hôm nay phải 5 lần submit mới xong, nhiều edge case quá
aTiUJyS.png


Làm theo cách thô thiển nhất, so sánh từng kí tự của s với danh sách word, nếu khớp thì tiếp tục, không khớp thì trả về false ngay và luôn.

C#:
public class Solution {
    string s;
    Dictionary<char, List<string>> dict;
    bool?[] memo;

    public bool WordBreak(string s, IList<string> wordDict) {
        this.s = s;
        memo = new bool?[s.Length];
        dict = new ();
        for (var c = 'a'; c <= 'z'; c++)
            dict[c] = new List<string>();
        foreach (var word in wordDict.OrderByDescending(word => word.Length))
            dict[word[0]].Add(word);

        return solve(0);
    }

    public bool solve(int i)
    {
        if (i >= s.Length)
            return true;
        if (memo[i] != null)
            return memo[i].Value;

        foreach (var word in dict[s[i]])
        {
            int j = word.Length - 1;
            for (; j >= 0 && i+j < s.Length; j--)
            {
                if (word[j] != s[i+j])
                    break;
            }
            //Console.WriteLine($"{word}, {j}, {i}");
            if (j < 0 && solve(i+word.Length))
            {
                memo[i] = true;
                return true;
            }
        }
        memo[i] = false;
        return false;
    }
}
 
C#:
public class Solution {
    public bool WordBreak(string s, IList<string> wordDict) {
        var dp = new bool[s.Length];
        for(int i = 0; i< dp.Length; i++)
        {
            foreach(var item in wordDict)
            {
                if(i  < item.Length - 1)
                    continue;

                if(i == item.Length - 1 || dp[i - item.Length])
                {
                    if (s.Substring(i - item.Length + 1, item.Length).Equals(item)) {
                        dp[i] = true;   
                        break;
                    }
                }
            }
        }

        return dp[s.Length - 1];
    }
}
Fen cày explore xong chưa

via theNEXTvoz for iPhone
 
Fen cày explore xong chưa

via theNEXTvoz for iPhone
Chưa fence ơi, 3 tuần vừa rồi đu theo Neetcode giải gần xong list 150 rồi. Còn mấy bài Graph + Dp nữa quay lại Exolore coi như ôn luyện thôi.
Luyện theo Neetcode ổn vãi, 3 tuần rồi giải thêm được 70 câu Medium rồi. Sắp lên được 200 câu :adore:
Giải hết Dp lại quay lại reset explore cày lại cho nhớ :beauty:

via theNEXTvoz for iPhone
 
Chưa fence ơi, 3 tuần vừa rồi đu theo Neetcode giải gần xong list 150 rồi. Còn mấy bài Graph + Dp nữa quay lại Exolore coi như ôn luyện thôi.
Luyện theo Neetcode ổn vãi, 3 tuần rồi giải thêm được 70 câu Medium rồi. Sắp lên được 200 câu :adore:
Giải hết Dp lại quay lại reset explore cày lại cho nhớ :beauty:

via theNEXTvoz for iPhone
Fen mua premium rồi à, t cũng mua mà chưa học 🥹

via theNEXTvoz for iPhone
 
Python:
class Solution:
    def wordBreak(self, s: str, wordDict: List[str]) -> bool:
        
        @cache
        def dfs(curIdx: int) -> bool:
            if curIdx == len(s):
                return True
            
            
            for word in wordDict:
                if s.startswith(word, curIdx):
                    if dfs(curIdx + len(word)):
                        return True
            
            return False
        
        return dfs(0)
 
C++:
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        int dp[n];
        memset(dp, false, sizeof(dp));
        for(int i = 0; i < n; ++i){
            for(string& word : wordDict){
                int l = word.size();
                // out of word or s[0...i - l] cant be constructed
                if(i - l + 1 < 0 || (i >= l && !dp[i - l])) continue;
                if(s.substr(i - l + 1, l) == word) dp[i] = true;
            }
        }
        return dp[n - 1];
    }
};
 
E mới làm bài leetcode ez mà mất phải hơn 4 tiếng @@ các bác cho e hỏi làm sao để cải thiện vậy ạ (bài đầu tiên của e làm luôn )
 
C++:
struct Solution {
    bool wordBreak(string_view s, const vector<string>& wordDict) {
        vector<bool> able(s.size() + 1, false);
        able[0] = true;
        for (size_t i = 0; i < s.size(); ++i) {
            if (!able[i]) continue;
            for (const auto& word : wordDict)
                if (s.substr(i).starts_with(word)) able[i + word.size()] = true;
        }
        return able[s.size()];
    }
};
 
C++:
class Solution {
public:
    bool wordBreak(string s, vector<string>& wordDict) {
        int n = s.size();
        int dp[n];
        memset(dp, false, sizeof(dp));
        for(int i = 0; i < n; ++i){
            for(string& word : wordDict){
                int l = word.size();
                // out of word or s[0...i - l] cant be constructed
                if(i - l + 1 < 0 || (i >= l && !dp[i - l])) continue;
                if(s.substr(i - l + 1, l) == word) dp[i] = true;
            }
        }
        return dp[n - 1];
    }
};
if(i - l + 1 <0 || (i>=l && !dp[i-l])) continue;
Bác giải thích giúp mình dòng này với. Tks bác
 
if(i - l + 1 <0 || (i>=l && !dp[i-l])) continue;
Bác giải thích giúp mình dòng này với. Tks bác
Mình có comment rồi nì
// out of word or s[0...i - l] cant be constructed
Đại đoại nếu ghép được từ word thì đầu tiên là phải tồn tại sub string s[0...i - l]
Hi vọng bác hiểu :D

Mà mấy bác khác dùng dp cũng làm tương tự, nhưng có thêm early return (break) nên có vẻ efficient hơn. Mình quên mất cái này.
 
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.212.535
Quay lại
Lên đầu trang