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

边栏推荐
- Keil5 MDK's formatting code tool and adding shortcuts
- To brush the video, it's better to see if you have mastered these interview questions. Slowly accumulating a monthly income of more than 10000 is not a dream.
- 刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
- Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
- Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
- Expanded polystyrene (EPS) global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
- 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
- “Hello IC World”
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
猜你喜欢

5 minutes to master machine learning iris logical regression classification

数字电路基础(五)算术运算电路

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

数字电路基础(三)编码器和译码器

How to learn automated testing in 2022? This article tells you

王爽汇编语言学习详细笔记一:基础知识

Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class

Interview Essentials: what is the mysterious framework asking?

MySQL development - advanced query - take a good look at how it suits you

四元数---基本概念(转载)
随机推荐
[pointer] find the largest string
My first blog
Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
CSAPP家庭作业答案7 8 9章
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
【指针】求解最后留下的人
Global and Chinese markets for complex programmable logic devices 2022-2028: Research Report on technology, participants, trends, market size and share
Want to learn how to get started and learn software testing? I'll give you a good chat today
“人生若只如初见”——RISC-V
Query method of database multi table link
CSAPP homework answers chapter 789
ucore lab8 文件系统 实验报告
1. Payment system
函数:用牛顿迭代法求方程的根
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
[HCIA continuous update] advanced features of routing
数字电路基础(二)逻辑代数
Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
王爽汇编语言学习详细笔记一:基础知识
使用 flask_whooshalchemyplus jieba实现flask的全局搜索