当前位置:网站首页>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
边栏推荐
- 王爽汇编语言学习详细笔记一:基础知识
- servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
- “Hello IC World”
- Keil5 MDK's formatting code tool and adding shortcuts
- Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
- “人生若只如初见”——RISC-V
- Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
- 数字电路基础(二)逻辑代数
- Want to learn how to get started and learn software testing? I'll give you a good chat today
- 数字电路基础(三)编码器和译码器
猜你喜欢
Software testing interview summary - common interview questions
1. Payment system
Es full text index
关于交换a和b的值的四种方法
ES全文索引
Fundamentals of digital circuits (I) number system and code system
“Hello IC World”
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
CSAPP家庭作業答案7 8 9章
Opencv recognition of face in image
随机推荐
Global and Chinese market for antiviral coatings 2022-2028: Research Report on technology, participants, trends, market size and share
C language learning summary (I) (under update)
Differences between select, poll and epoll in i/o multiplexing
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
Fundamentals of digital circuit (V) arithmetic operation circuit
CSAPP家庭作業答案7 8 9章
[pointer] use the insertion sorting method to arrange n numbers from small to large
ByteDance ten years of experience, old bird, took more than half a year to sort out the software test interview questions
指針:最大值、最小值和平均值
Four methods of exchanging the values of a and B
DVWA exercise 05 file upload file upload
Global and Chinese markets of cobalt 2022-2028: Research Report on technology, participants, trends, market size and share
Report on the double computer experiment of scoring system based on 485 bus
Global and Chinese markets of PIM analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
flask实现强制登陆
【指针】八进制转换为十进制
指针 --按字符串相反次序输出其中的所有字符
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
数字电路基础(三)编码器和译码器
函数:用牛顿迭代法求方程的根