当前位置:网站首页>The simple problem of leetcode is to split a string into several groups of length K
The simple problem of leetcode is to split a string into several groups of length K
2022-07-05 22:02:00 【·Starry Sea】
subject
character string s It can be divided into several lengths according to the following steps k Group :
The first group consists of the first... In the string k Characters make up , The second group consists of the following k It's a string , And so on . Every character can be One of them Part of a group .
For the last group , If the remaining characters of the string Insufficient k individual , Characters required fill To complete this set of characters .
Be careful , Remove the last group of characters fill( If it exists ) And connect all groups in order , The resulting string should be s .
Give you a string s , And the length of each group k And a character for filling fill , After processing according to the above steps , Returns an array of strings , The array represents s After grouping Composition of each group .
Example 1:
Input :s = “abcdefghi”, k = 3, fill = “x”
Output :[“abc”,“def”,“ghi”]
explain :
front 3 The characters are “abc” , Form the first group .
Next 3 The characters are “def” , Form a second group .
Last 3 The characters are “ghi” , Form a third group .
Because all groups can be completely filled with characters in the string , So you don't need to use padding characters .
therefore , formation 3 Group , Namely “abc”、“def” and “ghi” .
Example 2:
Input :s = “abcdefghij”, k = 3, fill = “x”
Output :[“abc”,“def”,“ghi”,“jxx”]
explain :
Similar to the previous example , Form the first three groups “abc”、“def” and “ghi” .
For the last group , There are only characters left in the string ‘j’ It can be used . To complete this group , Use padding characters ‘x’ two .
therefore , formation 4 Group , Namely “abc”、“def”、“ghi” and “jxx” .
Tips :
1 <= s.length <= 100
s It's only made up of lowercase letters
1 <= k <= 100
fill It's a small English letter
source : Power button (LeetCode)
Their thinking
You can first fill the string with a length of k Multiple , Then three three separate .
class Solution:
def divideString(self, s: str, k: int, fill: str) -> List[str]:
return re.findall('\w{'+str(k)+'}',s if len(s)%k==0 else s+fill*(k-len(s)%k))

边栏推荐
- How to view Apache log4j 2 remote code execution vulnerability?
- Advantages of robot framework
- 【愚公系列】2022年7月 Go教学课程 004-Go代码注释
- Sentinel production environment practice (I)
- 多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
- Create a virtual machine on VMware (system not installed)
- matlab绘制hsv色轮图
- Yolov5 training custom data set (pycharm ultra detailed version)
- Talking about MySQL index
- Implementation technology of recovery
猜你喜欢

AD637 usage notes

Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer

An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017

Win11运行cmd提示“请求的操作需要提升”的解决方法

华为游戏多媒体调用切换房间方法出现异常Internal system error. Reason:90000017

Web3为互联网带来了哪些改变?

华为快游戏调用登录接口失败,返回错误码 -1

MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server

Emotional analysis of wechat chat records on Valentine's day based on Text Mining

Summary of concurrency control
随机推荐
Poj3414 extensive search
MMAP learning
Bitbucket installation configuration
How to view Apache log4j 2 remote code execution vulnerability?
Shell script, awk uses if, for process control
Basic grammar of interview (Part 1)
ICMP 介绍
华为快游戏调用登录接口失败,返回错误码 -1
Poj 3237 Tree (Tree Chain Split)
AD637使用笔记
Huawei game multimedia service calls the method of shielding the voice of the specified player, and the error code 3010 is returned
How to add new fields to mongodb with code (all)
Detailed explanation of memset() function usage
Ad637 notes d'utilisation
Interprocess communication in the "Chris Richardson microservice series" microservice architecture
初级软件测试必问面试题
Serializability of concurrent scheduling
总结出现2xx、3xx、4xx、5xx状态码的原因
微服務鏈路風險分析
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer