当前位置:网站首页>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]))*' '
边栏推荐
- MySQL development - advanced query - take a good look at how it suits you
- Quaternion -- basic concepts (Reprint)
- Flash implements forced login
- Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
- 关于交换a和b的值的四种方法
- Stc-b learning board buzzer plays music
- The common methods of servlet context, session and request objects and the scope of storing data in servlet.
- 1. Payment system
- Summary of thread implementation
- Fundamentals of digital circuits (III) encoder and decoder
猜你喜欢
Login the system in the background, connect the database with JDBC, and do small case exercises
Quaternion -- basic concepts (Reprint)
Query method of database multi table link
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
Stc-b learning board buzzer plays music
“Hello IC World”
DVWA exercise 05 file upload file upload
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
随机推荐
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
基于485总线的评分系统双机实验报告
{1,2,3,2,5} duplicate checking problem
数据库多表链接的查询方式
Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
王爽汇编语言学习详细笔记一:基础知识
C language do while loop classic Level 2 questions
数字电路基础(五)算术运算电路
[pointer] find the value of the largest element in the two-dimensional array
函数:用牛顿迭代法求方程的根
数字电路基础(四) 数据分配器、数据选择器和数值比较器
"If life is just like the first sight" -- risc-v
ucore lab1 系统软件启动过程 实验报告
5分钟掌握机器学习鸢尾花逻辑回归分类
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Install and run tensorflow object detection API video object recognition system of Google open source
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
“Hello IC World”