当前位置:网站首页>Find the maximum 3 same digits in the string
Find the maximum 3 same digits in the string
2022-06-11 19:09:00 【AlbertOS】
introduce
Give you a string num , Represents a large integer . If an integer satisfies all of the following conditions , The integer is considered to be a High quality integer :
- The integer is num One length of the is 3 Of Substring .
- The integer is repeated by a unique number 3 Secondary composition .
Returns... As a string The largest good integer . If there is no integer that meets the requirements , Then return an empty string “” .
Be careful :
- Substring Is a sequence of consecutive characters in a string .
- num Or high-quality integers may exist Leading zero .
Example
Example 1:
Input :num = “6777133339”
Output :“777”
explain :num There are two high-quality integers in :“777” and “333” .
“777” It's the biggest one , So back “777” .
Example 2:
Input :num = “2300019”
Output :“000”
explain :“000” Is the only good integer .
Example 3:
Input :num = “42352338”
Output :“”
explain : There is no length of 3 An integer consisting of only one unique number . therefore , There are no good integers .
solution
My god ~ Such a simple problem, the official should use enumeration method ( Enumerating string num All lengths in are 3 The string of , And record the substring with the largest value required by the symbol ), It's a bit superfluous , I thought it would be faster to match the string directly , Anyway, a total of 9 Substring .
C++ Code :
class Solution {
public:
string largestGoodInteger(string num) {
for(int i=9 ; i>=0;i--){
string s(3,'0'+i);
if(num.find(s)!= string::npos){
return s; // If the string find If it is not empty, at least one is found ( Start with the biggest )
}
}
return "";
}
};
Official enumeration
The official idea is to enumerate strings num \textit{num} num All lengths in are 3 3 3 The string of , And record the substring that meets the requirements and has the largest corresponding value .
In particular , We use a string with an empty initial value res \textit{res} res To maintain the maximum number of matching substrings , At the same time, the traverse length from left to right is 3 3 3 The starting subscript of the substring i i i, Every time we traverse to a new subscript i i i, We judge by substring num [ i . . i + 2 ] \textit{num}[i..i + 2] num[i..i+2] Whether it is composed of the same characters : If yes, the substring meets the requirements , We will res \textit{res} res Updated to res \textit{res} res And the larger value of the substring ( Here, the size relationship of the string dictionary order is consistent with that of the corresponding integer ); If not, do nothing .
Final , If there is a substring that meets the requirements , be res \textit{res} res That is, the substring with the largest corresponding value ; If it doesn't exist , be res \textit{res} res Is an empty string . So we go back to res \textit{res} res As the answer .
class Solution {
public:
string largestGoodInteger(string num) {
int n = num.size();
string res;
for (int i = 0; i < n - 2; ++i) {
if (num[i] == num[i+1] && num[i+1] == num[i+2]) {
res = max(res, num.substr(i, 3));
}
}
return res;
}
};
summary
The disadvantage of the official enumeration method is that it will loop whether you find it or not n-3 Time ,
That is, the best and worst time complexity are O ( n ) O(n) O(n)
, The space complexity is O ( 1 ) O(1) O(1)
And we go from 999 The best time complexity to start string matching is to find... The first time O ( 1 ) O(1) O(1), The worst time complexity is O ( n 2 ) O(n^2) O(n2)( Although there is no regulation find Efficiency of function , We will match each time ), The space complexity is O ( 1 ) O(1) O(1)
Oh my god !! Careless, no flash , It's still the official force after analysis , Do not call the substring lookup function , The balance of time and space is achieved by enumerating directly , The enumeration method should be better than the official method when the string is disordered , But if the matching string appears in the early stage, the efficiency of my algorithm will be obviously improved , Each has its own merits 
边栏推荐
- [Multisim Simulation] generate square wave and triangular wave generators by operational amplifier
- leetcode:剑指 Offer 56 - II. 数组中数字出现的次数 II【简单排序】
- ASEMI的MOS管25N120在不同应用场景的表现
- Flash ckeditor rich text compiler can upload and echo images of articles and solve the problem of path errors
- SQL injection vulnerability learning 1: phpstudy integrated environment building DVWA shooting range
- On the selection and design of management subsystem of collection system
- On the translation of rich text storage database format
- 【信号去噪】基于非线性滤波器实现语音自适应去噪附matlab代码
- 让我们的坦克欢快的动起来吧
- Kubernetes binary installation (v1.20.15) (IX) closeout: deploy several dashboards
猜你喜欢

【图像去噪】基于绝对差分中值滤波、加权中值滤波法、改进加权中值滤波实现脉冲噪声图像去噪附matlab代码

Replace the backbone of target detection (take the fast RCNN as an example)

记录一下phpstudy配置php8.0和php8.1扩展redis
![cf:A. Print a Pedestal (Codeforces logo?) [simple traversal simulation]](/img/5e/0acd39d572954e5ecb936f49511d94.png)
cf:A. Print a Pedestal (Codeforces logo?) [simple traversal simulation]

5g communication test manual based on Ti am5728 + artix-7 FPGA development board (dsp+arm)

Financial bank_ Introduction to collection system

What is the workflow of dry goods MapReduce?
![[Multisim Simulation] using operational amplifier to generate sawtooth wave](/img/98/27086746dc552ada25fd36a82cb52b.png)
[Multisim Simulation] using operational amplifier to generate sawtooth wave

Visual slam lecture notes-10-1

【信号去噪】基于FFT和FIR实现信号去噪附matlab代码
随机推荐
视觉SLAM十四讲笔记-10-1
ASEMI的MOS管24N50参数,24N50封装,24N50尺寸
用户组的操作
Construct enemy tanks
己方坦克发射子弹
Crop disease detection using image processing technology and convolutional neural network (CNN)
Teach you how to learn the first set and follow set!!!! Hematemesis collection!! Nanny level explanation!!!
干货丨MapReduce的工作流程是怎样的?
开发中必备的文件的上传与下载
2022-2023 MEM pre approval interview notice of School of management, Xi'an Jiaotong University
On the selection and design of management subsystem of collection system
Cf:e. price maximization [sort + take mod + double pointer + pair]
Gmail: how do I recall an outgoing message?
关于我的 “二进制部署 kubernetes 集群” 的体验
[solution] codeforces round 798 (Div. 2)
司空见惯 - 会议室名称
Mysql深入完全学习---阶段1---学习总述
Cf:d. black and white stripe
MBA, EMBA, MPa, MEM, pre interview (pre interview) time batch of national colleges and universities has been released (continuously updated) - Wendu Management Institute
软件开发的整体流程