当前位置:网站首页>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))
边栏推荐
- Poj 3237 Tree (Tree Chain Split)
- Win11运行cmd提示“请求的操作需要提升”的解决方法
- 科技云报道:算力网络,还需跨越几道坎?
- Robot operation mechanism
- K210 learning notes (IV) k210 runs multiple models at the same time
- [Yugong series] go teaching course in July 2022 004 go code Notes
- How to organize an actual attack and defense drill
- NET中小型企业项目开发框架系列(一个)
- Blocking protocol for concurrency control
- ICMP 介绍
猜你喜欢
Oracle检查点队列–实例崩溃恢复原理剖析
Efficiency difference between row first and column first traversal of mat data types in opencv
元宇宙中的三大“派系”
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
总结出现2xx、3xx、4xx、5xx状态码的原因
Index optimization of performance tuning methodology
MMAP learning
Defect detection - Halcon surface scratch detection
多家呼吸机巨头产品近期被一级召回 呼吸机市场仍在增量竞争
Shell script, awk uses if, for process control
随机推荐
Reptile practice
Blocking of concurrency control
PIP install beatifulsoup4 installation failed
AD637使用笔记
深信服X计划-网络协议基础 DNS
Basic grammar of interview (Part 1)
Poj 3237 Tree (Tree Chain Split)
Summarize the reasons for 2XX, 3xx, 4xx, 5xx status codes
HDU 4391 Paint The Wall 段树(水
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
EBS Oracle 11g cloning steps (single node)
Serializability of concurrent scheduling
Form artifact
Server optimization of performance tuning methodology
华为快游戏调用登录接口失败,返回错误码 -1
MySQL连接断开报错MySQLdb._exceptions.OperationalError 4031, The client was disconnected by the server
Hysbz 2243 staining (tree chain splitting)
Two stage locking protocol for concurrency control
Database recovery strategy
如何開發引入小程序插件