当前位置:网站首页>Collating strings
Collating strings
2022-07-27 04:08:00 【Sharp blade CC】
1544. Sorting strings
The difficulty is simple 46
Give you a string of uppercase and lowercase English letters s .
In a sorted string , Two adjacent characters s[i] and s[i+1], among 0<= i <= s.length-2 , The following conditions must be met :
- if
s[i]Is a lowercase character , bes[i+1]Cannot be the same uppercase character . - if
s[i]Is an uppercase character , bes[i+1]Lowercase characters can be different .
Please arrange the strings , Each time you can choose from the string that meets the above conditions Two adjacent Character and delete , Until the string is sorted .
Please return to the sorted character string . The problem is guaranteed under the given constraints , The answer corresponding to the test sample is unique .
** Be careful :** Empty strings are also sorted strings , Although there are no characters in it .
Example 1:
Input :s = "leEeetcode"
Output :"leetcode"
explain : Whether you choose... For the first time i = 1 still i = 2, Will make "leEeetcode" Reduced to "leetcode" .
Example 2:
Input :s = "abBAcC"
Output :""
explain : There are many different situations , But all situations lead to the same result . for example :
"abBAcC" --> "aAcC" --> "cC" --> ""
"abBAcC" --> "abBA" --> "aA" --> ""
Example 3:
Input :s = "s"
Output :"s"
Tips :
1 <= s.length <= 100sOnly lowercase and uppercase letters
Train of thought :【 Stack 】
Using the idea of stacks , Create a new string **tmp ** Used to store the characters to be compared , First of all s First character of push_back To tmp in , Then on s Start traversing other characters in , Then there will be the following two situations :
- If tmp Top of stack , That is to say tmp The end of the string is A lowercase letter Words , be s[ i ] Cannot be uppercase of the same character , If so, then tmp The trailing element of the string pop fall .
- If tmp Top of stack , That is to say tmp The end of the string is Capitalization Words , be s[ i ] Cannot be lowercase of the same character , If so, then tmp The trailing element of the string pop fall .
class Solution {
public:
string makeGood(string s) {
string tmp;
tmp.push_back(s[0]); // take s The first element of push To tmp Tail of
for(int i = 1; i < s.size(); ++i)
{
if(s[i] + 32 == tmp.back() || s[i] - 32 == tmp.back())
{
tmp.pop_back();
}
else
{
tmp += s[i];
}
}
return tmp;
}
};
Train of thought two : Direct traversal
Take it directly s[ i ] And s[ i + 1 ] Compare , If the same case occurs , Then the elements in these two positions are directly erase fall , Then go back to the beginning of the string to traverse . The time complexity of this method is relatively high .
class Solution {
public:
string makeGood(string s) {
int n = s.size();
int i = 0;
while(i < n - 1)
{
if(s[i] >= 'a' && s[i] <= 'z')
{
if((s[i] - 32) == s[i+1])
{
s.erase(i, 2);
i = 0;
continue;
}
}
else
{
if((s[i] + 32) == s[i+1])
{
s.erase(i, 2);
i = 0;
continue;
}
}
++i;
}
return s;
}
};
边栏推荐
- VR全景人淘金“小心机”(上)
- There is no problem reading from flick CDC to mysql8 and mysql5. What should I do?
- [OBS] dynamic bit rate: bit rate estimation
- URDF_Xcaro
- Plato farm is expected to further expand its ecosystem through elephant swap
- Want to get the Apache official domain name mailbox? Exclusive interview with Apache linkis five new committers to tell you how to do it
- 二叉树的坡度
- NFT digital collection system development: old brand literary magazines play with trendy Digital Collections
- [Yugong series] July 2022 go teaching course 018 switch of branch structure
- Function pointer and callback function
猜你喜欢

Function pointer and callback function

Subject 3: Jinan Zhangqiu line 6

2022危险化学品生产单位安全生产管理人员考试题模拟考试题库模拟考试平台操作

「Gonna Be Alright 会好的」数藏现已开售!感受艺术家的心灵共鸣

3381. 手机键盘(清华大学考研机试题)

Development of NFT digital collection system: Xiaoyi digital intelligence helps brands launch NFT with one click on the chain

NFT digital collection system development: old brand literary magazines play with trendy Digital Collections

452页13万字现代智慧乡镇雪亮工程整体解决方案2022版

科目三: 济南章丘6号线

科目三: 济南章丘三号线
随机推荐
Development of NFT digital collection system: Xiaoyi digital intelligence helps brands launch NFT with one click on the chain
面试题 02.05. 链表求和
Golang发送邮件库email
路由策略第一关
面试题 16.05. 阶乘尾数
flink cdc 到MySQL8没问题,到MySQL5读有问题,怎么办?
Threads and processes
A. YES or YES?
科目三: 济南章丘三号线
【obs】动态码率:码率估算
288 page 180000 word intelligent campus overall design directory
Use tag tag in golang structure
LeetCode 第二十八天
Message queue learning -- Concepts
Read and understand | how data center supports enterprise digital operation
暑假加餐|有钱人和你想的不一样(第5天)+电力系统潮流仿真(文档和Matlab代码)
NFT digital collection system development: old brand literary magazines play with trendy Digital Collections
使用redis c库,异步内存泄露的问题
Towhee weekly model
Subject 3: Jinan Zhangqiu line 2