当前位置:网站首页>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]))*' '
边栏推荐
- [pointer] octal to decimal
- Quaternion -- basic concepts (Reprint)
- Description of Vos storage space, bandwidth occupation and PPS requirements
- 5 minutes to master machine learning iris logical regression classification
- 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?
- Don't you even look at such a detailed and comprehensive written software test question?
- 【指针】查找最大的字符串
- Es full text index
- Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Opencv recognition of face in image
数字电路基础(四) 数据分配器、数据选择器和数值比较器
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
Don't you even look at such a detailed and comprehensive written software test question?
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Summary of thread implementation
Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
Stc-b learning board buzzer plays music
Quaternion -- basic concepts (Reprint)
Matplotlib绘图快速入门
随机推荐
How to transform functional testing into automated testing?
四元数---基本概念(转载)
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
China's county life record: go upstairs to the Internet, go downstairs' code the Great Wall '
【指针】求二维数组中最大元素的值
后台登录系统,JDBC连接数据库,做小案例练习
【指针】数组逆序重新存放后并输出
[pointer] solve the last person left
[pointer] find the largest string
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
数字电路基础(一)数制与码制
Numpy快速上手指南
Interview Essentials: what is the mysterious framework asking?
Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Fundamentals of digital circuits (III) encoder and decoder
Fundamentals of digital circuits (I) number system and code system
[pointer] the array is stored in reverse order and output