当前位置:网站首页>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]))*' '
边栏推荐
- Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
- 函数:用牛顿迭代法求方程的根
- Face and eye recognition based on OpenCV's own model
- How to learn automated testing in 2022? This article tells you
- Get started with Matplotlib drawing
- 函数:计算字符串中大写字母的个数
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
- What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
- 【指针】求解最后留下的人
猜你喜欢
5分钟掌握机器学习鸢尾花逻辑回归分类
基于485总线的评分系统双机实验报告
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
ES全文索引
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
1. Payment system
Report on the double computer experiment of scoring system based on 485 bus
Fundamentals of digital circuits (III) encoder and decoder
随机推荐
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
Functions: Finding Roots of equations
[oiclass] share prizes
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
Oracle foundation and system table
Es full text index
[pointer] counts the number of times one string appears in another string
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
Query method of database multi table link
Numpy快速上手指南
Global and Chinese markets for complex programmable logic devices 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
STC-B学习板蜂鸣器播放音乐2.0
基于485总线的评分系统双机实验报告
STC-B学习板蜂鸣器播放音乐
1. Payment system
Public key box