当前位置:网站首页>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
边栏推荐
- How to learn automated testing in 2022? This article tells you
- 【指针】使用插入排序法将n个数从小到大进行排列
- Flash implements forced login
- Interview Essentials: what is the mysterious framework asking?
- 函数:用牛顿迭代法求方程的根
- 后台登录系统,JDBC连接数据库,做小案例练习
- Description of Vos storage space, bandwidth occupation and PPS requirements
- 数字电路基础(五)算术运算电路
- Pointer -- output all characters in the string in reverse order
- 关于交换a和b的值的四种方法
猜你喜欢
5 minutes to master machine learning iris logical regression classification
Login the system in the background, connect the database with JDBC, and do small case exercises
CSAPP家庭作业答案7 8 9章
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
数字电路基础(二)逻辑代数
Four methods of exchanging the values of a and B
About the garbled code problem of superstar script
Software testing interview summary - common interview questions
Build your own application based on Google's open source tensorflow object detection API video object recognition system (II)
数字电路基础(三)编码器和译码器
随机推荐
Global and Chinese market of portable and handheld TVs 2022-2028: Research Report on technology, participants, trends, market size and share
Why can swing implement a form program by inheriting the JFrame class?
Logstack introduction and deployment -- elasticstack (elk) work notes 019
Oracle foundation and system table
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
[oiclass] maximum formula
150 common interview questions for software testing in large factories. Serious thinking is very valuable for your interview
CSAPP家庭作业答案7 8 9章
Statistics 8th Edition Jia Junping Chapter 1 after class exercises and answers summary
Expanded polystyrene (EPS) global and Chinese markets 2022-2028: technology, participants, trends, market size and share Research Report
ucore lab8 文件系统 实验报告
C language learning summary (I) (under update)
Summary of thread implementation
【指针】删除字符串s中的所有空格
How to solve the poor sound quality of Vos?
Es full text index
Four methods of exchanging the values of a and B
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 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
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008