当前位置:网站首页>Interview question 01.06. string compression
Interview question 01.06. string compression
2022-07-28 01:41:00 【Welcome to the confrontation Road】
Subject requirements
String compression . The number of times characters are used to repeat , Write a method , Realize basic string compression function . such as , character string aabcccccaaa Will turn into a2b1c5a3. if “ Compress ” The string after does not get shorter , Returns the original string . You can assume that the string contains only uppercase and lowercase letters (a to z).
Example 1:
Input :"aabcccccaaa"
Output :"a2b1c5a3"
Example 2:
Input :"abbccd"
Output :"abbccd"
explain :"abbccd" Compressed as "a1b2c2d1", Longer than the original string length .
Tips :
The string length is [0, 50000] Within the scope of
class Solution {
public:
string compressString(string S) {
int len = S.size();
string s1;
int count = 0;
for(int i = 0; i < len; i++)// Traversal string
{
count++;// Number of times used to record letters
if(S[i] != S[i+1])// If the current letter is different from the next letter , Then store the letter and the number of times of the letter in s1 in
{
s1+=S[i];
s1 += to_string(count);
count = 0;
}
}
if(len > s1.size())
return s1;
else
return S;
}
};
Experience
to_string(2), Convert numbers to character form
边栏推荐
- Count the number of given strings in a string
- 软件测试面试题:如何发现数据库的相关问题?
- Three instance
- Content bypass sharing
- 实现OCR语言识别Demo(二)- 图片及识别内容的展现和交互
- Day 013 一维数组练习
- HRD 1. 一个简单而靠谱的HRD的检测方法
- MySQL进阶--存储过程以及自定义函数
- Can ordinary equipment access TSN time sensitive network?
- Software test interview question: how to find problems related to the database?
猜你喜欢

“你“想当测试/开发程序员吗?努力发芽的我们......

Lecture 16 of project practice: using the open close principle to realize the commodity price rule engine

8000 word explanation of OBSA principle and application practice

Unity Shader入门精要学习——基础纹理

MATLAB 44种动漫渐变色绘图程序

股票问题5连

Introduction to routing strategy

8000字讲透OBSA原理与应用实践

LeetCode 2347. 最好的扑克手牌

了解Shader
随机推荐
总结:Prometheus存储
Codeforces summer training weekly (7.14~7.20)
MATLAB 44种动漫渐变色绘图程序
Qt 绘制系统简介
面试题 01.05. 一次编辑
内容bypass分享
Lua advanced
8000字讲透OBSA原理与应用实践
LeetCode 2347. 最好的扑克手牌
QT setting Icon
Introduction to routing strategy
I want to get 20K after 3 years of experience, but I haven't got it for half a month?
数仓搭建——DWS层
Let's move forward together, the 10th anniversary of Google play!
Flutter--密码登录注册界面
Shutter -- password login registration interface
C语言·指针
Brushes and brushes
路由策略简介
Three basic teaching