当前位置:网站首页>LeetCode-1405. Longest happy string
LeetCode-1405. Longest happy string
2022-06-12 06:23:00 【Border wanderer】
If the string does not contain any 'aaa','bbb' or 'ccc' Such a string is used as a substring , Then the string is a 「 Happy string 」.
Here are three integers a,b ,c, Please return Any one A string that satisfies all of the following conditions s:
s Is a happy string as long as possible .
s in most Yes a Letters 'a'、b Letters 'b'、c Letters 'c' .
s It only contains 'a'、'b' 、'c' Three letters .
If such a string does not exist s , Please return an empty string "".
Example 1:
Input :a = 1, b = 1, c = 7
Output :"ccaccbcc"
explain :"ccbccacc" It is also a correct answer .
Example 2:
Input :a = 2, b = 2, c = 1
Output :"aabbc"
Example 3:
Input :a = 7, b = 1, c = 0
Output :"aabaa"
explain : This is the only correct answer to the test case .
Tips :
0 <= a, b, c <= 100
a + b + c > 0
analysis :
Adopt a greedy strategy , Select the longest character each time (max(a, b, c)) As a continuum , Then take less as the middle partition .
This ensures that the happy string is the longest .
#include<iostream>
#include<unordered_map>
using namespace std;
class Solution {
public:
string longestDiverseString(int a, int b, int c) {
string str;
char ch;
unordered_map<char, int> mp;
mp['a'] = a;
mp['b'] = b;
mp['c'] = c;
ch = maxch(mp['a'], mp['b'], mp['c']);
while (mp[ch] != 0) {
construct(mp[ch], ch, str);
if (maxch(mp['a'], mp['b'], mp['c']) == ch) {
if (savech(ch, mp['a'], mp['b'], mp['c'], str)) {
ch = maxch(mp['a'], mp['b'], mp['c']);
continue;
}
else {
break;
}
}
else {
ch = maxch(mp['a'], mp['b'], mp['c']);
}
}
return str;
}
bool savech(char ch, int& a, int& b, int& c, string& str) {
bool issucc = true;
if (ch != 'a' && a > 0) {
str += 'a';
a--;
}
else if (ch != 'b' && b > 0) {
str += 'b';
b--;
}
else if (ch != 'c' && c > 0) {
str += 'c';
c--;
}
else {
issucc = false;
}
return issucc;
}
char maxch(int a, int b, int c) {
if (a >= b) {
if (a >= c) {
return 'a';
}
else {
return 'c';
}
}
else if (c >= b) {
return 'c';
}
else {
return 'b';
}
}
void construct(int& num, char c, string& str) {
if (num >= 2) {
num = num - 2;
str += c;
str += c;
}
else if (num == 1) {
num = num - 1;
str += c;
}
}
};
int main() {
Solution* ps = new Solution();
unique_ptr<Solution> ptr(new Solution());
cout << ps->longestDiverseString(7, 1, 0) << endl;
system("pause");
return 0;
}边栏推荐
- Multithreading (4) -- no lock (3) -- longadder source code
- Mastering UI development with unity
- Directx11 advanced tutorial tiled based deffered shading
- 分段贝塞尔曲线
- GET 和 POST 的区别及留言板代码实现
- Pytorch implementation of regression model
- Script for unity3d to recursively search for a node with a specific name from all child nodes of a node
- Leetcode-553. Optimal division
- Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
- English grammar_ Adverb_ With or without ly, the meaning is different
猜你喜欢

AI operation ch8

Houdini script vex learning

Poisson disk sampling for procedural placement

MNIST handwritten data recognition by RNN

MLP sensor

Trunet: short videos generation from long videos via story preserving truncation (thesis translation)

leetcode 35. Search insert location

LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格

Logistic regression model

QT--实现TCP通信
随机推荐
C2w model - language model
Leetcode January 12 daily question 334 Increasing ternary subsequence
Single channel picture reading
Redis configuration (III) -- master-slave replication
LeetCode-1185. Day of the week
In unity3d, billboard effect can be realized towards another target
JS预解析
Word2Vec
Introduction to the method of diligently searching for the alliance procedure
Qt-- realize TCP communication
获取图片的尺寸
Sqlite Cross - compile Dynamic Library
Open the camera in unity3d and display the contents of the camera in the scene as texture2d
SQLite cross compile dynamic library
468. verifying the IP address
Unity surface shader with template buffer
C # converts the hexadecimal code form of text to text (ASCII)
Summary of some problems in sensor bring up
Leetcode-1512. Number of good pairs
MLP sensor