给定两个长度为N 的字符串S 1和S 2和一个正整数K ,任务是找到字典序上最小的字符串,使其与给定的两个字符串S 1和S 2在恰好K 个位置不同。如果不存在这样的字符串,则打印“-1” 。
例子:
Input: N = 4, K = 3, S1 = “ccbb”, S2 = “caab”
Output: abcb
Explanation:
String “abcb” differs from S1 at exactly 3 places i.e., at positions 1, 2 and 3, and
String “abcb” differs from S2 at exactly 3 places i.e., at positions 1, 2 and 3.
Input: N = 5, K = 1, S1 = “cbabb”, S2 = “babaa”
Output: -1
Explanation:
No such string exists that simultaneously differs from S1 and S2 only at 1 position.
方法:
- 不是从头开始构建 S 3 ,而是选择 S 2作为结果字符串S 3并尝试根据给定的约束对其进行修改。
- 找出答案字符串S 3与 S 1在多少个位置上不同。
- 让它们在恰好d 个位置彼此不同。然后,其中的答案字符串S 3能够从两个不同字符串的地方最小数目为地方小区(d / 2)和最大数目可以是N。
- 如果K在范围[ceil(d/2), N] 内,则 S 3存在,否则 S 3不存在并打印“-1” 。
- 下面是字符串S 3的情况:
- K大于d
- 如果 K 大于 d,则修改 S 1不同于 S 3 的所有位置,以便修改后,该位置的 S 3现在将不同于 S 1和 S 2 。递减 K。
- 修改 S 1与 S 3相同的位置,这样修改后,该位置的 S 3现在将不同于 S 1和 S 2 。递减 K。
- K小于或等于d
- 在这种情况下,仅修改 S 1和 S 3不同的 S 3位置。
- 修改后,设X为仅 S 1与 S 3不同且 S 2与 S 3不同的位置数。
- 令T为S 1和S 2与S 3不同的位置数。那么方程将是:
- K大于d
(2 * X) + T = d
X + T = K
- 求解这些方程,可以得到X和T的值,并相应地修改答案字符串S 3 。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
char arr[] = { 'a', 'b', 'c' };
// Function to find the string which
// differ at exactly K positions
void findString(int n, int k,
string s1, string s2)
{
// Initialise s3 as s2
string s3 = s2;
// Number of places at which
// s3 differ from s2
int d = 0;
for (int i = 0;
i < s1.size(); i++) {
if (s1[i] != s2[i])
d++;
}
// Minimum possible value
// is ceil(d/2) if it is
// not possible then -1
if ((d + 1) / 2 > k) {
cout << "-1" << endl;
return;
}
else {
// Case 2 when K is
// less equal d
if (k <= d) {
// value of X and T
// after solving
// equations
// T shows the number
// of modification
// such that position
// differ from both
// X show the modification
// such that this position
// only differ from only
// one string
int X = d - k;
int T = 2 * k - d;
for (int i = 0;
i < s3.size(); i++) {
if (s1[i] != s2[i]) {
// modify the position
// such that this
// differ from both
// S1 & S2 & decrease
// the T at each step
if (T > 0) {
// Finding the character
// which is different
// from both S1 and S2
for (int j = 0;
j < 3; j++) {
if (arr[j] != s1[i]
&& arr[j] != s2[i]) {
s3[i] = arr[j];
T--;
break;
}
}
}
// After we done T
// start modification
// to meet our
// requirement
// for X type
else if (X > 0) {
s3[i] = s1[i];
X--;
}
}
}
// Resultant string
cout << s3 << endl;
}
else {
// Case 1 when K > d
// In first step, modify all
// the character which are
// not same in S1 and S3
for (int i = 0;
i < s1.size(); i++) {
if (s1[i] != s3[i]) {
for (int j = 0;
j < 3; j++) {
// Finding character
// which is
// different from
// both S1 and S2
if (arr[j] != s1[i]
&& arr[j] != s3[i]) {
s3[i] = arr[j];
k--;
break;
}
}
}
}
// Our requrement not
// satisied by performing
// step 1. We need to
// modify the position
// which matches in
// both string
for (int i = 0;
i < s1.size(); i++) {
if (s1[i] == s3[i] && k) {
// Finding the character
// which is different
// from both S1 and S2
for (int j = 0; j < 3; j++) {
if (arr[j] != s1[i]
&& arr[j] != s3[i]) {
s3[i] = arr[j];
k--;
break;
}
}
}
}
// Resultant string
cout << s3 << endl;
}
}
}
// Driver Code
int main()
{
int N = 4, k = 2;
// Given two strings
string S1 = "zzyy";
string S2 = "zxxy";
// Function Call
findString(N, k, S1, S2);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
static char arr[] = { 'a', 'b', 'c' };
// Function to find the String which
// differ at exactly K positions
static void findString(int n, int k,
char []s1,
char []s2)
{
// Initialise s3 as s2
char []s3 = s2;
// Number of places at which
// s3 differ from s2
int d = 0;
for (int i = 0;
i < s1.length; i++)
{
if (s1[i] != s2[i])
d++;
}
// Minimum possible value
// is Math.ceil(d/2) if it is
// not possible then -1
if ((d + 1) / 2 > k)
{
System.out.print("-1" + "\n");
return;
}
else
{
// Case 2 when K is
// less equal d
if (k <= d)
{
// value of X and T
// after solving
// equations
// T shows the number
// of modification
// such that position
// differ from both
// X show the modification
// such that this position
// only differ from only
// one String
int X = d - k;
int T = 2 * k - d;
for (int i = 0; i < s3.length; i++)
{
if (s1[i] != s2[i])
{
// modify the position
// such that this
// differ from both
// S1 & S2 & decrease
// the T at each step
if (T > 0)
{
// Finding the character
// which is different
// from both S1 and S2
for (int j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s2[i])
{
s3[i] = arr[j];
T--;
break;
}
}
}
// After we done T
// start modification
// to meet our
// requirement
// for X type
else if (X > 0)
{
s3[i] = s1[i];
X--;
}
}
}
// Resultant String
System.out.print(new String(s3) + "\n");
}
else
{
// Case 1 when K > d
// In first step, modify all
// the character which are
// not same in S1 and S3
for (int i = 0;
i < s1.length; i++)
{
if (s1[i] != s3[i])
{
for (int j = 0;
j < 3; j++)
{
// Finding character
// which is
// different from
// both S1 and S2
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break;
}
}
}
}
// Our requrement not
// satisied by performing
// step 1. We need to
// modify the position
// which matches in
// both String
for (int i = 0;
i < s1.length; i++)
{
if (s1[i] == s3[i] && k > 0)
{
// Finding the character
// which is different
// from both S1 and S2
for (int j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break;
}
}
}
}
// Resultant String
System.out.print(new String(s3) + "\n");
}
}
}
// Driver Code
public static void main(String[] args)
{
int N = 4, k = 2;
// Given two Strings
String S1 = "zzyy";
String S2 = "zxxy";
// Function Call
findString(N, k, S1.toCharArray(), S2.toCharArray());
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 program for the above approach
arr = [ 'a', 'b', 'c' ]
# Function to find the string which
# differ at exactly K positions
def findString(n, k, s1, s2):
# Initialise s3 as s2
s3 = s2
s3 = list(s3)
# Number of places at which
# s3 differ from s2
d = 0
for i in range(len(s1)):
if (s1[i] != s2[i]):
d += 1
# Minimum possible value
# is ceil(d/2) if it is
# not possible then -1
if ((d + 1) // 2 > k):
print ("-1")
return
else:
# Case 2 when K is
# less equal d
if (k <= d):
# Value of X and T
# after solving
# equations
# T shows the number
# of modification
# such that position
# differ from both
# X show the modification
# such that this position
# only differ from only
# one string
X = d - k
T = 2 * k - d
for i in range(len(s3)):
if (s1[i] != s2[i]):
# Modify the position
# such that this
# differ from both
# S1 & S2 & decrease
# the T at each step
if (T > 0):
# Finding the character
# which is different
# from both S1 and S2
for j in range(3):
if (arr[j] != s1[i] and
arr[j] != s2[i]):
s3[i] = arr[j]
T -= 1
break
# After we done T
# start modification
# to meet our
# requirement
# for X type
elif (X > 0):
s3[i] = s1[i]
X -= 1
# Resultant string
print("".join(s3))
else:
# Case 1 when K > d
# In first step, modify all
# the character which are
# not same in S1 and S3
for i in range(len(s1)):
if (s1[i] != s3[i]):
for j in range(3):
# Finding character
# which is
# different from
# both S1 and S2
if (arr[j] != s1[i] and
arr[j] != s3[i]):
s3[i] = arr[j]
k -= 1
break
# Our requrement not
# satisied by performing
# step 1. We need to
# modify the position
# which matches in
# both string
for i in range(len(s1)):
if (s1[i] == s3[i] and k):
# Finding the character
# which is different
# from both S1 and S2
for j in range (3):
if (arr[j] != s1[i] and
arr[j] != s3[i]):
s3[i] = arr[j]
k -= 1
break
# Resultant string
print("".join(s3))
# Driver Code
if __name__ == "__main__":
N = 4
k = 2
# Given two strings
S1 = "zzyy"
S2 = "zxxy"
# Function call
findString(N, k, S1, S2)
# This code is contributed by chitranayal
C#
// C# program for the above approach
using System;
class GFG{
static char []arr = { 'a', 'b', 'c' };
// Function to find the String which
// differ at exactly K positions
static void findString(int n, int k,
char []s1,
char []s2)
{
// Initialise s3 as s2
char []s3 = s2;
// Number of places at which
// s3 differ from s2
int d = 0;
for(int i = 0;
i < s1.Length; i++)
{
if (s1[i] != s2[i])
d++;
}
// Minimum possible value
// is Math.Ceiling(d/2) if it is
// not possible then -1
if ((d + 1) / 2 > k)
{
Console.Write("-1" + "\n");
return;
}
else
{
// Case 2 when K is
// less equal d
if (k <= d)
{
// Value of X and T
// after solving
// equations
// T shows the number
// of modification
// such that position
// differ from both
// X show the modification
// such that this position
// only differ from only
// one String
int X = d - k;
int T = 2 * k - d;
for(int i = 0; i < s3.Length; i++)
{
if (s1[i] != s2[i])
{
// Modify the position
// such that this
// differ from both
// S1 & S2 & decrease
// the T at each step
if (T > 0)
{
// Finding the character
// which is different
// from both S1 and S2
for(int j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s2[i])
{
s3[i] = arr[j];
T--;
break;
}
}
}
// After we done T start
// modification to meet our
// requirement for X type
else if (X > 0)
{
s3[i] = s1[i];
X--;
}
}
}
// Resultant String
Console.Write(new String(s3) + "\n");
}
else
{
// Case 1 when K > d
// In first step, modify all
// the character which are
// not same in S1 and S3
for(int i = 0; i < s1.Length; i++)
{
if (s1[i] != s3[i])
{
for(int j = 0; j < 3; j++)
{
// Finding character
// which is different
// from both S1 and S2
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break;
}
}
}
}
// Our requrement not
// satisied by performing
// step 1. We need to
// modify the position
// which matches in
// both String
for(int i = 0; i < s1.Length; i++)
{
if (s1[i] == s3[i] && k > 0)
{
// Finding the character
// which is different
// from both S1 and S2
for(int j = 0; j < 3; j++)
{
if (arr[j] != s1[i] &&
arr[j] != s3[i])
{
s3[i] = arr[j];
k--;
break;
}
}
}
}
// Resultant String
Console.Write(new String(s3) + "\n");
}
}
}
// Driver Code
public static void Main(String[] args)
{
int N = 4, k = 2;
// Given two Strings
String S1 = "zzyy";
String S2 = "zxxy";
// Function Call
findString(N, k, S1.ToCharArray(),
S2.ToCharArray());
}
}
// This code is contributed by amal kumar choubey
Javascript
输出:
zaay
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live