当前位置:网站首页>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;
}边栏推荐
- 摄像头拍摄运动物体,产生运动模糊/拖影的原因分析
- C2w model - language model
- Video based fire smoke detection using robust AdaBoost
- [word] word 2010 recording macro batch replacing paragraph marks in the selected text
- Script for unity3d to recursively search for a node with a specific name from all child nodes of a node
- 哈工大信息内容安全实验
- Nodemon cannot load the file c:\users\administrator\appdata\roaming\npm\nodemon PS1, because script execution is prohibited in this system
- 夜神模擬器adb查看log
- Storing texture2d to hard disk JPG file with script under unity3d
- Redis application (I) -- distributed lock
猜你喜欢

IBL of directx11 advanced tutorial PBR (3)

AI operation ch8

勤于奋寻找联盟程序方法介绍

Houdini & UE4 programmed generation of mountains and multi vegetation scattering points

RNN model

Sqlite Cross - compile Dynamic Library

Word2Vec

夜神模拟器adb查看log

Bert Chinese classification model training + reasoning + deployment

Using hidden Markov model to mark part of speech
随机推荐
Introduction to the method of diligently searching for the alliance procedure
Redis queue
2D human pose estimation for pose estimation - pifpaf:composite fields for human pose estimation
Android studio mobile development creates a new database and obtains picture and text data from the database to display on the listview list
Piecewise Bezier curve
Nocturnal simulator ADB view log
zip 和.items()区别
. Net core and Net framework comparison
获取图片的尺寸
Excel VBA opens a file that begins with the specified character
N-degree Bessel curve
LeetCode-1078. Bigram participle
姿态估计之2D人体姿态估计 - PifPaf:Composite Fields for Human Pose Estimation
Leetcode-1512. Number of good pairs
Highlight detection with pairwise deep ranking for first person video summary (thesis translation)
LeetCode个人题解(剑指offer3-5)3.数组中重复的数字,4.二维数组中的查找,5.替换空格
JS pre parsing
Mastering UI development with unity
Computer composition and design work06 —— 基于MIPS
Simple spiral ladder generation for Houdini program modeling