当前位置:网站首页>Rearrange spaces between words in leetcode simple questions
Rearrange spaces between words in leetcode simple questions
2022-07-06 15:03:00 【·Starry Sea】
subject
Give you a string text , The string consists of several words surrounded by spaces . Each word consists of one or more lowercase English letters , And there is at least one space between two words . Topic test cases guarantee text Contain at least one word .
Please rearrange the spaces , Make the number of spaces between each pair of adjacent words equal , And try to Maximize The number . If you can't redistribute all spaces equally , please Place extra spaces at the end of the string , This also means that the returned string should be the same as the original text The length of the string is equal .
return Rearrange the string after the spaces .
Example 1:
Input :text = " this is a sentence "
Output :“this is a sentence”
explain : All in all 9 A space and 4 Word . Can be 9 Spaces are evenly distributed between adjacent words , The number of spaces between adjacent words is :9 / (4-1) = 3 individual .
Example 2:
Input :text = " practice makes perfect"
Output :“practice makes perfect "
explain : All in all 7 A space and 3 Word .7 / (3-1) = 3 A space plus 1 An extra space . Extra spaces need to be placed at the end of the string .
Example 3:
Input :text = “hello world”
Output :“hello world”
Example 4:
Input :text = " walks udp package into bar a”
Output :"walks udp package into bar a "
Example 5:
Input :text = “a”
Output :“a”
Tips :
1 <= text.length <= 100
text Consists of lowercase letters and ’ ’ form
text Contains at least one word
source : Power button (LeetCode)
Their thinking
Find the words in the sentence separately , Then calculate the number of spaces and distribute the spaces to each word according to the requirements of the topic .
class Solution:
def reorderSpaces(self, text: str) -> str:
words=re.findall(r'\S+',text)
if len(words)>1:
a,b=divmod(text.count(' '),len(words)-1)
return (' '*a).join(words)+b*' '
else:
return words[0]+(len(text)-len(words[0]))*' '

边栏推荐
- Install and run tensorflow object detection API video object recognition system of Google open source
- Using flask_ Whooshalchemyplus Jieba realizes global search of flask
- Keil5-MDK的格式化代码工具及添加快捷方式
- C language learning summary (I) (under update)
- CSAPP家庭作业答案7 8 9章
- Query method of database multi table link
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
- ucore lab8 文件系统 实验报告
- Global and Chinese market of pinhole glossmeter 2022-2028: Research Report on technology, participants, trends, market size and share
- [HCIA continuous update] working principle of static route and default route
猜你喜欢

1.支付系统

Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class

Description of Vos storage space, bandwidth occupation and PPS requirements

“人生若只如初见”——RISC-V

DVWA exercise 05 file upload file upload

Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
![Cadence physical library lef file syntax learning [continuous update]](/img/0b/75a4ac2649508857468d9b37703a27.jpg)
Cadence physical library lef file syntax learning [continuous update]

STC-B学习板蜂鸣器播放音乐

基于485总线的评分系统双机实验报告

Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
随机推荐
Es full text index
Oracle foundation and system table
数据库多表链接的查询方式
Common Oracle commands
[oiclass] share prizes
【指针】求字符串的长度
后台登录系统,JDBC连接数据库,做小案例练习
How to transform functional testing into automated testing?
“人生若只如初见”——RISC-V
Logstack introduction and deployment -- elasticstack (elk) work notes 019
ucore lab8 文件系统 实验报告
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
基于485总线的评分系统双机实验报告
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
[pointer] find the largest string
Pointers: maximum, minimum, and average
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
Interview Essentials: what is the mysterious framework asking?
Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class