当前位置:网站首页>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))
边栏推荐
- Cross end solutions to improve development efficiency
- Bitbucket installation configuration
- Implementing Lmax disruptor queue from scratch (IV) principle analysis of multithreaded producer multiproducersequencer
- Some common processing problems of structural equation model Amos software
- Storage optimization of performance tuning methodology
- 深信服X计划-网络协议基础 DNS
- Evolution of large website architecture and knowledge system
- Codeforces 12D ball tree array simulation 3 sorting elements
- How can Huawei online match improve the success rate of player matching
- Type of fault
猜你喜欢
Huawei fast game failed to call the login interface, and returned error code -1
Database recovery strategy
深信服X计划-网络协议基础 DNS
Implementation technology of recovery
ICMP 介绍
资深电感厂家告诉你电感什么情况会有噪音电感噪音是比较常见的一种电感故障情况,如果使用的电感出现了噪音大家也不用着急,只需要准确查找分析出什么何原因,其实还是有具体的方法来解决的。作为一家拥有18年品牌
Web3为互联网带来了哪些改变?
Type of fault
Bitbucket installation configuration
Granularity of blocking of concurrency control
随机推荐
Win11缺少dll文件怎么办?Win11系统找不到dll文件修复方法
Server optimization of performance tuning methodology
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
Efficiency difference between row first and column first traversal of mat data types in opencv
The Blue Bridge Cup web application development simulation competition is open for the first time! Contestants fast forward!
HDU 4391 paint the wall segment tree (water
Lightweight dynamic monitorable thread pool based on configuration center - dynamictp
Did you brush the real title of the blue bridge cup over the years? Come here and teach you to counter attack!
华为联机对战如何提升玩家匹配成功几率
EL与JSTL注意事项汇总
Hysbz 2243 staining (tree chain splitting)
Overview of concurrency control
K210 learning notes (IV) k210 runs multiple models at the same time
微服務鏈路風險分析
MMAP学习
Robot operation mechanism
Robot framework setting variables
Installation of VMware Workstation
Recovery technology with checkpoints
EBS Oracle 11g 克隆步骤(单节点)