当前位置:网站首页>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
边栏推荐
- What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
- Pointer -- output all characters in the string in reverse order
- How to use Moment. JS to check whether the current time is between 2 times
- HackTheBox-Emdee five for life
- Keil5-MDK的格式化代码工具及添加快捷方式
- Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
- [pointer] octal to decimal
- Global and Chinese market of goat milk powder 2022-2028: Research Report on technology, participants, trends, market size and share
- DVWA exercise 05 file upload file upload
- Global and Chinese market of DVD recorders 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
随机推荐
Why can swing implement a form program by inheriting the JFrame class?
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
[Ogg III] daily operation and maintenance: clean up archive logs, register Ogg process services, and regularly back up databases
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
Global and Chinese market of maleic acid modified rosin esters 2022-2028: Research Report on technology, participants, trends, market size and share
指针--剔除字符串中的所有数字
数字电路基础(三)编码器和译码器
Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
[pointer] find the value of the largest element in the two-dimensional array
函数:求两个正数的最大公约数和最小公倍
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
“Hello IC World”
Matplotlib绘图快速入门
【指针】求二维数组中最大元素的值
Soft exam information system project manager_ Project set project portfolio management --- Senior Information System Project Manager of soft exam 025
【指针】查找最大的字符串
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
指针 --按字符串相反次序输出其中的所有字符
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)