当前位置:网站首页>Capitalize the title of leetcode simple question
Capitalize the title of leetcode simple question
2022-07-06 15:04:00 【·Starry Sea】
subject
Give you a string title , It consists of a single space connecting one or more words , Each word contains only English letters . Please put the first letter of each word according to the following rules Capitalization :
If the length of the word is 1 perhaps 2 , All letters in lowercase .
otherwise , Capitalize the first letter of the word , The remaining letters become lowercase .
Please return After capitalization Of title .
Example 1:
Input :title = “capiTalIze tHe titLe”
Output :“Capitalize The Title”
explain :
Because all words are at least 3 , Capitalize each word , The remaining letters become lowercase .
Example 2:
Input :title = “First leTTeR of EACH Word”
Output :“First Letter of Each Word”
explain :
word “of” The length is 2 , So it stays completely lowercase .
Other words are at least... Long 3 , So other words are capitalized , The remaining letters are lowercase .
Example 3:
Input :title = “i lOve leetcode”
Output :“i Love Leetcode”
explain :
word “i” The length is 1 , So it keeps lowercase .
Other words are at least... Long 3 , So other words are capitalized , The remaining letters are lowercase .
Tips :
1 <= title.length <= 100
title Words separated by a single space , And does not contain any leading or suffix spaces .
Each word consists of uppercase and lowercase letters , And are Non empty Of .
source : Power button (LeetCode)
Their thinking
about title Every word in , Replace case by condition .
class Solution:
def capitalizeTitle(self, title: str) -> str:
def change(matched):
word=matched.group('word')
return word.lower() if len(word)<3 else word[0].upper()+word[1:].lower()
return re.sub('(?P<word>\S+)',change,title) # Replace letters according to rules
边栏推荐
- Detailed introduction to dynamic programming (with examples)
- STC-B学习板蜂鸣器播放音乐2.0
- Login the system in the background, connect the database with JDBC, and do small case exercises
- 数字电路基础(二)逻辑代数
- Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
- How to solve the poor sound quality of Vos?
- The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
- Install and run tensorflow object detection API video object recognition system of Google open source
- Pointer -- eliminate all numbers in the string
- “Hello IC World”
猜你喜欢
数字电路基础(五)算术运算电路
Build your own application based on Google's open source tensorflow object detection API video object recognition system (I)
数据库多表链接的查询方式
HackTheBox-Emdee five for life
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer
ES全文索引
Summary of thread implementation
数字电路基础(二)逻辑代数
Want to learn how to get started and learn software testing? I'll give you a good chat today
Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
随机推荐
Global and Chinese markets of Iam security services 2022-2028: Research Report on technology, participants, trends, market size and share
"If life is just like the first sight" -- risc-v
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
Pointer -- eliminate all numbers in the string
Four methods of exchanging the values of a and B
【指针】统计一字符串在另一个字符串中出现的次数
四元数---基本概念(转载)
Statistics 8th Edition Jia Junping Chapter 2 after class exercises and answer summary
What are the business processes and differences of the three basic business modes of Vos: direct dial, callback and semi direct dial?
数字电路基础(四) 数据分配器、数据选择器和数值比较器
【指针】数组逆序重新存放后并输出
Matplotlib绘图快速入门
Es full text index
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
Global and Chinese markets for GaN on diamond semiconductor substrates 2022-2028: Research Report on technology, participants, trends, market size and share
函数:求两个正数的最大公约数和最小公倍
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
CSAPP homework answers chapter 789
Cc36 different subsequences