LeetCode: Word Break II

本文深入探讨了前端开发领域的关键技术,包括HTML、CSS、JavaScript等基础技术,以及Vue、React、Angular等现代框架的应用。通过实例解析,展示了如何利用这些技术构建高效、响应式的用户界面。此外,文章还涉及了前端工具如Webpack、Babel等的使用,以及前端最佳实践,旨在帮助开发者提升技能,构建高质量的Web应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word.

Return all such possible sentences.

For example, given
s = "catsanddog",
dict = ["cat", "cats", "and", "sand", "dog"].

A solution is ["cats and dog", "cat sand dog"].

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string> &dict) {
        
        vector<vector<string>> possible(s.length());
        stack<string> m_stack;
        
        for(int i=0; i < s.size(); i++)
        {
            for ( auto it = dict.begin(); it != dict.end(); ++it )
            {
                string subString = s.substr(i, (*it).size());
                if(subString == *it)
                {
                    possible[i + (*it).size() - 1].push_back(subString);
                }
            }
        }
        vector<string> cur;
        buildResult(possible, cur, s.size() - 1);
        return m_result;
       
    }
    void buildResult(vector<vector<string> > possible,  vector<string>& cur, int index)
    {
        if(index < 0)
        {
            string result = "";
            for(int i = cur.size() - 1; i >= 0; i--)
            {
                result += cur[i];
                if(i != 0)
                    result += ' ';
            }
            m_result.insert(m_result.begin(), result);
          
            return;
            
        }
        for(int i = 0; i < possible[index].size(); i++)
        {
            cur.push_back(possible[index][i]);
            buildResult(possible, cur, index - possible[index][i].size());
            cur.pop_back();
        }
    }
private:
    vector<string> m_result;
};


Round 2:

class Solution {
public:
    vector<string> wordBreak(string s, unordered_set<string>& wordDict) {
        bool possible[s.size()+1];
		memset(possible, 1, sizeof(possible));
		vector<string> result;
		string cur;
		dfs(s, wordDict, result, 0, cur, possible);
		return result;
    }
private:
	void dfs(string s, unordered_set<string> &wordDict, vector<string> &result, int start, string &cur, bool possible[])
	{
		int size = s.size();
		if(start == size)
		{
			result.push_back(cur.substr(0, cur.size()-1));
		}
		for(int i = start; i < size; i++)
		{
			string temp = s.substr(start, i-start+1);
			if(wordDict.find(temp) != wordDict.end() && possible[i+1])
			{
				cur += temp;
				cur += " ";
				int countSolution = result.size();
				dfs(s, wordDict, result, i+1, cur, possible);
				if(result.size() == countSolution)
					possible[i+1] = false;
				cur = cur.substr(0, cur.size() - temp.size() - 1);
			}
		}
	}
};


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值