当前位置:网站首页>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))
边栏推荐
- Index optimization of performance tuning methodology
- Two stage locking protocol for concurrency control
- Analysis and test of ModbusRTU communication protocol
- PyGame practical project: write Snake games with 300 lines of code
- K210 learning notes (IV) k210 runs multiple models at the same time
- Poj 3237 Tree (Tree Chain Split)
- DBeaver同时执行多条insert into报错处理
- 深信服X计划-网络协议基础 DNS
- 从零开始实现lmax-Disruptor队列(四)多线程生产者MultiProducerSequencer原理解析
- 854. 相似度为 K 的字符串 BFS
猜你喜欢
深信服X计划-网络协议基础 DNS
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
MMAP learning
Sentinel production environment practice (I)
[Yugong series] go teaching course in July 2022 004 go code Notes
Getting started with microservices (resttemplate, Eureka, Nacos, feign, gateway)
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
科技云报道荣膺全球云计算大会“云鼎奖”2013-2022十周年特别贡献奖
Countdown to 92 days, the strategy for the provincial preparation of the Blue Bridge Cup is coming~
Business learning of mall commodity module
随机推荐
科技云报道:算力网络,还需跨越几道坎?
Comment développer un plug - in d'applet
MySQL disconnection reports an error MySQL ldb_ exceptions. OperationalError 4031, The client was disconnected by the server
数博会精彩回顾 | 彰显科研实力,中创算力荣获数字化影响力企业奖
【愚公系列】2022年7月 Go教学课程 004-Go代码注释
Analyse des risques liés aux liaisons de microservices
Oracle checkpoint queue - Analysis of the principle of instance crash recovery
Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
"Chris Richardson microservices series" uses API gateway to build microservices
Defect detection - Halcon surface scratch detection
Learning of mall permission module
Microservice link risk analysis
An exception occurred in Huawei game multimedia calling the room switching method internal system error Reason:90000017
Win11运行cmd提示“请求的操作需要提升”的解决方法
Granularity of blocking of concurrency control
Storage optimization of performance tuning methodology
Shell script, awk uses if, for process control
Hysbz 2243 staining (tree chain splitting)
[Yugong series] go teaching course in July 2022 004 go code Notes