当前位置:网站首页>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]))*' '
边栏推荐
- [pointer] solve the last person left
- With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
- Want to learn how to get started and learn software testing? I'll give you a good chat today
- How to learn automated testing in 2022? This article tells you
- MySQL development - advanced query - take a good look at how it suits you
- Database monitoring SQL execution
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
- ES全文索引
- flask实现强制登陆
- [oiclass] maximum formula
猜你喜欢
Matplotlib绘图快速入门
Fundamentals of digital circuits (II) logic algebra
“人生若只如初见”——RISC-V
CSAPP家庭作业答案7 8 9章
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
Summary of thread implementation
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
ES全文索引
MySQL development - advanced query - take a good look at how it suits you
随机推荐
【指针】求解最后留下的人
Pointeurs: maximum, minimum et moyenne
HackTheBox-Emdee five for life
函数:求方程的根
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Install and run tensorflow object detection API video object recognition system of Google open source
Get started with Matplotlib drawing
[issue 18] share a Netease go experience
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
"If life is just like the first sight" -- risc-v
[pointer] find the largest string
Numpy Quick Start Guide
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
Keil5-MDK的格式化代码工具及添加快捷方式
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
指針:最大值、最小值和平均值
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
【指针】使用插入排序法将n个数从小到大进行排列
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
[pointer] find the value of the largest element in the two-dimensional array