当前位置:网站首页>2027. Minimum number of operations to convert strings

2027. Minimum number of operations to convert strings

2022-07-06 16:07:00 mrbone9

Address :

Power button icon-default.png?t=M0H8https://leetcode-cn.com/problems/minimum-moves-to-convert-string/

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)
link :https://leetcode-cn.com/problems/minimum-moves-to-convert-string
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .

Ideas :

Flop once yes 3 Elements , If the first element is 'O', It's a waste of opportunity , So looking for elements directly is 'X'  The beginning of

Every time 3 One for a group , Till the end , Pay attention to the lower boundary

Method 1 、 find 'X' At the beginning 3 Tuples

int minimumMoves(char * s){
	int slen = strlen(s);
	int moves = 0;
	
	int i=0;
	while(s[i])
	{
		if(s[i] == 'O')
			i++;
		else
		{
			if( (i+3) <= slen )
			{
				moves++;
				i += 3;
			}
			else
			{
				moves++;
				break;
			}
		}
	}
	
	return moves;
}

See more brush notes

原网站

版权声明
本文为[mrbone9]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202131317036899.html