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

边栏推荐
- Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
- [pointer] find the length of the string
- Report on the double computer experiment of scoring system based on 485 bus
- [pointer] find the value of the largest element in the two-dimensional array
- 指针 --按字符串相反次序输出其中的所有字符
- “人生若只如初见”——RISC-V
- 5分钟掌握机器学习鸢尾花逻辑回归分类
- Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
- 1.支付系统
- How to learn automated testing in 2022? This article tells you
猜你喜欢

CSAPP家庭作业答案7 8 9章

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

STC-B学习板蜂鸣器播放音乐

Interview Essentials: what is the mysterious framework asking?

150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview

数字电路基础(四) 数据分配器、数据选择器和数值比较器

Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class

数据库多表链接的查询方式

Es full text index

Fundamentals of digital circuits (I) number system and code system
随机推荐
Detailed introduction to dynamic programming (with examples)
Want to learn how to get started and learn software testing? I'll give you a good chat today
Fundamentals of digital circuit (V) arithmetic operation circuit
Differences between select, poll and epoll in i/o multiplexing
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
函数:计算字符串中大写字母的个数
Function: find the root of the equation by Newton iterative method
【指针】统计一字符串在另一个字符串中出现的次数
指针:最大值、最小值和平均值
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
“Hello IC World”
Global and Chinese markets of MPV ACC ECU 2022-2028: Research Report on technology, participants, trends, market size and share
“人生若只如初见”——RISC-V
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
Using flask_ Whooshalchemyplus Jieba realizes global search of flask
关于交换a和b的值的四种方法
函数:求方程的根