当前位置:网站首页>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
边栏推荐
- MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
- 数字电路基础(四) 数据分配器、数据选择器和数值比较器
- Pointer -- output all characters in the string in reverse order
- Stc-b learning board buzzer plays music 2.0
- 数字电路基础(二)逻辑代数
- [pointer] counts the number of times one string appears in another string
- [pointer] use the insertion sorting method to arrange n numbers from small to large
- Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
- Keil5-MDK的格式化代码工具及添加快捷方式
- Practical cases, hand-in-hand teaching you to build e-commerce user portraits | with code
猜你喜欢
Software testing interview summary - common interview questions
Stc-b learning board buzzer plays music 2.0
数字电路基础(二)逻辑代数
王爽汇编语言详细学习笔记二:寄存器
The salary of testers is polarized. How to become an automated test with a monthly salary of 20K?
MySQL development - advanced query - take a good look at how it suits you
Fundamentals of digital circuits (III) encoder and decoder
Interview Essentials: what is the mysterious framework asking?
Database monitoring SQL execution
ucore lab2 物理内存管理 实验报告
随机推荐
Global and Chinese market of barrier thin film flexible electronics 2022-2028: Research Report on technology, participants, trends, market size and share
1. Payment system
What is the transaction of MySQL? What is dirty reading and what is unreal reading? Not repeatable?
HackTheBox-Emdee five for life
“人生若只如初见”——RISC-V
【指针】求字符串的长度
Function: find the maximum common divisor and the minimum common multiple of two positive numbers
Statistics, 8th Edition, Jia Junping, Chapter 11 summary of knowledge points of univariate linear regression and answers to exercises after class
刷视频的功夫,不如看看这些面试题你掌握了没有,慢慢积累月入过万不是梦。
Vysor uses WiFi wireless connection for screen projection_ Operate the mobile phone on the computer_ Wireless debugging -- uniapp native development 008
Global and Chinese markets of electronic grade hexafluorobutadiene (C4F6) 2022-2028: Research Report on technology, participants, trends, market size and share
C language do while loop classic Level 2 questions
[oiclass] share prizes
Wang Shuang's detailed notes on assembly language learning I: basic knowledge
Zhejiang University Edition "C language programming experiment and exercise guide (3rd Edition)" topic set
CSAPP homework answers chapter 789
Report on the double computer experiment of scoring system based on 485 bus
1.支付系统
【指针】求二维数组中最大元素的值
What level do 18K test engineers want? Take a look at the interview experience of a 26 year old test engineer