当前位置:网站首页>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]))*' '

边栏推荐
- "If life is just like the first sight" -- risc-v
- Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
- 数字电路基础(一)数制与码制
- Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
- [HCIA continuous update] advanced features of routing
- Function: calculates the number of uppercase letters in a string
- CSAPP家庭作業答案7 8 9章
- The four connection methods of JDBC are directly coded
- Fundamentals of digital circuits (I) number system and code system
- Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

数字电路基础(二)逻辑代数

1.支付系统

The common methods of servlet context, session and request objects and the scope of storing data in servlet.

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

"If life is just like the first sight" -- risc-v

Wang Shuang's detailed learning notes of assembly language II: registers

“Hello IC World”

About the garbled code problem of superstar script

Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class

ucore lab2 物理内存管理 实验报告
随机推荐
[HCIA continuous update] advanced features of routing
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
线程的实现方式总结
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
With 27K successful entry ByteDance, this "software testing interview notes" has benefited me for life
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
Function: find the root of the equation by Newton iterative method
Global and Chinese market of RF shielding room 2022-2028: Research Report on technology, participants, trends, market size and share
[oiclass] maximum formula
Differences between select, poll and epoll in i/o multiplexing
【指针】八进制转换为十进制
C language learning summary (I) (under update)
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
王爽汇编语言详细学习笔记二:寄存器
Oracle foundation and system table
My first blog
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
[pointer] delete all spaces in the string s
"If life is just like the first sight" -- risc-v