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

边栏推荐
- Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
- Want to learn how to get started and learn software testing? I'll give you a good chat today
- Software testing interview summary - common interview questions
- 函数:求方程的根
- Get started with Matplotlib drawing
- {1,2,3,2,5} duplicate checking problem
- Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
- Statistics 8th Edition Jia Junping Chapter IX summary of knowledge points of classified data analysis and answers to exercises after class
- 指针--剔除字符串中的所有数字
- Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

C language do while loop classic Level 2 questions

Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary

ucore lab8 文件系统 实验报告

移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)

Report on the double computer experiment of scoring system based on 485 bus

Logstack introduction and deployment -- elasticstack (elk) work notes 019

后台登录系统,JDBC连接数据库,做小案例练习

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

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

Description of Vos storage space, bandwidth occupation and PPS requirements
随机推荐
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
How to use Moment. JS to check whether the current time is between 2 times
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
Functions: Finding Roots of equations
线程的实现方式总结
[issue 18] share a Netease go experience
【指针】求字符串的长度
Four methods of exchanging the values of a and B
[pointer] find the value of the largest element in the two-dimensional array
Install and run tensorflow object detection API video object recognition system of Google open source
Function: calculates the number of uppercase letters in a string
四元数---基本概念(转载)
Fundamentals of digital circuits (I) number system and code system
数据库多表链接的查询方式
函数:用牛顿迭代法求方程的根
DVWA exercise 05 file upload file upload
[pointer] the array is stored in reverse order and output
Fundamentals of digital circuit (V) arithmetic operation circuit
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 4 Summary and after class exercise answers