|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
题目描述:
- 如果字符串中不含有任何 'aaa','bbb' 或 'ccc' 这样的字符串作为子串,那么该字符串就是一个「快乐字符串」。
- 给你三个整数 a,b ,c,请你返回 任意一个 满足下列全部条件的字符串 s:
- s 是一个尽可能长的快乐字符串。
- s 中 最多 有a 个字母 'a'、b 个字母 'b'、c 个字母 'c' 。
- s 中只含有 'a'、'b' 、'c' 三种字母。
- 如果不存在这样的字符串 s ,请返回一个空字符串 ""。
-  
- 示例 1:
- 输入:a = 1, b = 1, c = 7
- 输出:"ccaccbcc"
- 解释:"ccbccacc" 也是一种正确答案。
- 示例 2:
- 输入:a = 2, b = 2, c = 1
- 输出:"aabbc"
- 示例 3:
- 输入:a = 7, b = 1, c = 0
- 输出:"aabaa"
- 解释:这是该测试用例的唯一正确答案。
-  
- 提示:
- 0 <= a, b, c <= 100
- a + b + c > 0
- 来源:力扣(LeetCode)
- 链接:https://leetcode-cn.com/problems/longest-happy-string
- 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
复制代码
- class Solution {
- public:
- bool valid(string& res, int pos, string temp){
- string store = res;
- store.insert(pos, temp);
- for(int i = 0; i < store.size() - 2; i++){
- if(store[i] == temp[0] && store[i+1] == temp[0] && store[i+2] == temp[0])return false;
- }
- return true;
- }
- string longestDiverseString(int a, int b, int c) {
- string res;
- vector<pair<int, string> > store;
- store.push_back(make_pair(a, "a"));
- store.push_back(make_pair(b, "b"));
- store.push_back(make_pair(c, "c"));
- sort(store.begin(), store.end());
- string temp1 = "abc";
- int count1 = store[0].first;
- for(int i = 0; i < count1; i++)res += temp1;
- string temp2 = store[1].second + store[2].second;
- int count2 = store[1].first - store[0].first;
- for(int i = 0; i < count2; i++)res += temp2;
- string temp3 = store[2].second;
- int count3 = store[2].first - store[1].first;
- for(int i = 0; i < count3; i++){
- bool flag = false;
- for(int j = 0; j <= res.size(); j++){
- if(valid(res, j, temp3)){
- res.insert(j, temp3);
- flag = true;
- break;
- }
- }
- if(flag == false)break;
- }
- return res;
- }
- };
复制代码
参考链接:https://leetcode-cn.com/problems ... a-ru-by-time-limit/ |
|