当前位置:网站首页>The minimum number of operations to convert strings in leetcode simple problem
The minimum number of operations to convert strings in leetcode simple problem
2022-07-06 15:04:00 【·Starry Sea】
subject
Give you a string s , from n Characters make up , Each character is not ‘X’ Namely ‘O’ .
once operation Defined as from s Selected from Three consecutive characters And convert each selected character to ‘O’ . Be careful , If the character is already ‘O’ , Just keep unchanged .
Return to s All characters in are converted to ‘O’ executable least Operating frequency .
Example 1:
Input :s = “XXX”
Output :1
explain :XXX -> OOO
One operation , Select All 3 Characters , And turn them into ‘O’ .
Example 2:
Input :s = “XXOX”
Output :2
explain :XXOX -> OOOX -> OOOO
The first operation , Choose the former 3 Characters , And convert these characters to ‘O’ .
then , After selection 3 Characters , And perform the conversion . The resulting string consists entirely of characters ‘O’ form .
Example 3:
Input :s = “OOOO”
Output :0
explain :s There is no need to convert ‘X’ .
Tips :
3 <= s.length <= 1000
s[i] by ‘X’ or ‘O’
source : Power button (LeetCode)
Their thinking
Traversal string , Once found ‘X’ Just remember one operation , Then the pointer moves back three positions , If you encounter ‘O’ Just move once .
class Solution:
def minimumMoves(self, s: str) -> int:
count=0
i=0
while i<len(s):
if s[i]=='X':
count+=1
i+=3
else:
i+=1
return count

边栏推荐
- Four methods of exchanging the values of a and B
- CSAPP homework answers chapter 789
- Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
- [pointer] the array is stored in reverse order and output
- Get started with Matplotlib drawing
- Database monitoring SQL execution
- Investment operation steps
- Statistics 8th Edition Jia Junping Chapter 4 Summary and after class exercise answers
- 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.
- Description of Vos storage space, bandwidth occupation and PPS requirements
猜你喜欢

数字电路基础(二)逻辑代数

What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?

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

Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025

Description of Vos storage space, bandwidth occupation and PPS requirements

Fundamentals of digital circuit (V) arithmetic operation circuit
![[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases](/img/31/875b08d752ecd914f4e727e561adbd.jpg)
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases

Stc-b learning board buzzer plays music

5 minutes to master machine learning iris logical regression classification

ucore lab2 物理内存管理 实验报告
随机推荐
The common methods of servlet context, session and request objects and the scope of storing data in servlet.
ucore lab2 物理内存管理 实验报告
Stc-b learning board buzzer plays music
ucore lab1 系统软件启动过程 实验报告
Pointer -- output all characters in the string in reverse order
[issue 18] share a Netease go experience
Matplotlib绘图快速入门
[pointer] find the largest string
【指针】求解最后留下的人
“Hello IC World”
Cc36 different subsequences
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
【指针】统计一字符串在另一个字符串中出现的次数
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
CSAPP家庭作业答案7 8 9章
[pointer] the array is stored in reverse order and output
1. Payment system
How to use Moment. JS to check whether the current time is between 2 times
How to solve the poor sound quality of Vos?
HackTheBox-Emdee five for life