给定字符串“ 0”,“ 1”和“ 2”。任务是找到最少的替换数,以使相邻字符不相等。
例子:
Input: s = “201220211”
Output: 2
Resultant string after changes is 201210210
Input: s = “0120102”
Output: 0
方法:可以使用贪婪法解决以下问题。我们可以贪婪地比较每对相邻的货币对。如果相邻对,其是字符在第i个和第i-1相同,则更换第i个字符与未第i-1和i + 1个索引处等于所述字符的字符。如果是最后一对,则将字符替换为不等于第i-1个索引处的字符。
下面是上述方法的实现:
C++
// C++ program to count the minimal
// replacements such that adjacent characters
// are unequal
#include
using namespace std;
// Function to count the number of
// minimal replacements
int countMinimalReplacements(string s)
{
// Find the length of the string
int n = s.length();
int cnt = 0;
// Iterate in the string
for (int i = 1; i < n; i++) {
// Check if adjacent is similar
if (s[i] == s[i - 1]) {
cnt += 1;
// If not the last pair
if (i != (n - 1)) {
// Check for character which is
// not same in i+1 and i-1
for (auto it : "012") {
if (it != s[i + 1] &&
it != s[i - 1]) {
s[i] = it;
break;
}
}
}
else // Last pair
{
// Check for character which is
// not same in i-1 index
for (auto it : "012") {
if (it != s[i - 1]) {
s[i] = it;
break;
}
}
}
}
}
return cnt;
}
// Driver Code
int main()
{
string s = "201220211";
cout << countMinimalReplacements(s);
return 0;
}
Java
// Java program to count the minimal
// replacements such that adjacent
// characters are unequal
class GFG
{
static final int MAX = 26;
// Function to count the number of
// minimal replacements
static int countMinimalReplacements(char[] s)
{
// Find the length of the String
int n = s.length;
int cnt = 0;
// Iterate in the String
for (int i = 1; i < n; i++)
{
// Check if adjacent is similar
if (s[i] == s[i - 1])
{
cnt += 1;
// If not the last pair
if (i != (n - 1))
{
// Check for character which is
// not same in i+1 and i-1
for (char it : "012".toCharArray())
{
if (it != s[i + 1]
&& it != s[i - 1])
{
s[i] = it;
break;
}
}
}
else // Last pair
{
// Check for character which is
// not same in i-1 index
for (char it : "012".toCharArray())
{
if (it != s[i - 1])
{
s[i] = it;
break;
}
}
}
}
}
return cnt;
}
// Driver Code
public static void main(String[] args)
{
String s = "201220211";
System.out.println(countMinimalReplacements(s.toCharArray()));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python 3 program to count the minimal
# replacements such that adjacent
# characters are unequal
# Function to count the number of
# minimal replacements
def countMinimalReplacements(s):
# Find the length of the string
n = len(s)
cnt = 0
# Iterate in the string
for i in range(1, n):
# Check if adjacent is similar
if (s[i] == s[i - 1]):
cnt += 1;
# If not the last pair
if (i != (n - 1)):
# Check for character which is
# not same in i+1 and i-1
s = list(s)
for j in "012":
if (j != s[i + 1] and
j != s[i - 1]):
s[i] = j
break
s = ''.join(s)
# Last pair
else:
# Check for character which is
# not same in i-1 index
s = list(s)
for k in "012":
if (k != s[i - 1]):
s[i] = k
break
s = ''.join(s)
return cnt
# Driver Code
if __name__ == '__main__':
s = "201220211"
print(countMinimalReplacements(s))
# This code is contributed by
# Surendra_Gangwar
C#
// C# program to count the minimal
// replacements such that adjacent
// characters are unequal
using System;
class GFG
{
static readonly int MAX = 26;
// Function to count the number of
// minimal replacements
static int countMinimalReplacements(char[] s)
{
// Find the length of the String
int n = s.Length;
int cnt = 0;
// Iterate in the String
for (int i = 1; i < n; i++)
{
// Check if adjacent is similar
if (s[i] == s[i - 1])
{
cnt += 1;
// If not the last pair
if (i != (n - 1))
{
// Check for character which is
// not same in i+1 and i-1
foreach (char it in "012".ToCharArray())
{
if (it != s[i + 1] &&
it != s[i - 1])
{
s[i] = it;
break;
}
}
}
else // Last pair
{
// Check for character which is
// not same in i-1 index
foreach (char it in "012".ToCharArray())
{
if (it != s[i - 1])
{
s[i] = it;
break;
}
}
}
}
}
return cnt;
}
// Driver Code
public static void Main(String[] args)
{
String s = "201220211";
Console.WriteLine(countMinimalReplacements(s.ToCharArray()));
}
}
// This code is contributed by Rajput-Ji
PHP
Javascript
输出:
2
时间复杂度: O(n)
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。