通过替换给定字符串中的“ ?
给定一个由N个小写字符和字符'?'组成的字符串S和一个正整数K ,任务是替换每个字符'?'使用一些小写字母,使给定的字符串变为K的句点。如果无法这样做,则打印“-1” 。
A string is said to be a period of K if and only if the length of the string is a multiple of K and for all possible value of i over the range [0, K) the value S[i + K], S[i + 2*K], S[i + 3*K], …, remains the same.
例子:
Input: S = “ab??”, K = 2
Output: abab
Input: S = “??????”, K = 3
Output: aaaaaa
天真的方法:给定的方法也可以通过替换每个字符“?”来生成所有可能的字符串组合来解决。使用任何小写字符并打印每个大小为 K的子字符串相同的字符串。
时间复杂度: O(26 M ),其中M是'?'的数量在字符串S中。
辅助空间: O(1)
高效的方法:上述方法也可以通过遍历字符串的方式进行优化,使得遍历第一个、第二个、第三个等字符并且如果所有字符都是'?'然后将其替换为字符'a' ,否则,如果每个相应位置仅存在一个不同的字符,则替换为'?'使用该字符,否则,无法根据给定条件修改字符串,因此打印“-1”。请按照以下步骤解决问题:
- 使用变量i在[0, K]范围内迭代循环并执行以下步骤:
- 初始化一个映射,比如M以存储位置i的子字符串字符的频率。
- 使用增量为K的变量j在范围[i, N]上遍历给定字符串,并将字符S[j]的频率按1存储在映射M中。
- 完成上述步骤后,执行以下操作:
- 如果地图的大小大于2 ,则打印“-1”并跳出循环。
- 否则,如果地图的大小为2 ,则替换每个'?'与那个不同的字符。
- 否则,替换所有的“?”带有字符'a' 。
- 完成上述步骤后,如果可以修改字符串,则打印字符串S作为结果字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include "bits/stdc++.h"
using namespace std;
// Function to modify the given string
// such that string S is a period of K
string modifyString(string& S, int K)
{
int N = S.length();
// Iterate over the range [0, K]
for (int i = 0; i < K; i++) {
// Stores the frequency of the
// characters S[i + j*K]
map M;
// Iterate over the string with
// increment of K
for (int j = i; j < N; j += K) {
M[S[j]]++;
}
// Print "-1"
if (M.size() > 2) {
return "-1";
}
else if (M.size() == 1) {
if (M['?'] != 0) {
// Replace all characters
// of the string with '?'
// to 'a' to make it smallest
for (int j = i; j < N; j += K) {
S[j] = 'a';
}
}
}
// Otherwise
else if (M.size() == 2) {
char ch;
// Find the character other
// than '?'
for (auto& it : M) {
if (it.first != '?') {
ch = it.first;
}
}
// Replace all characters
// of the string with '?'
// to character ch
for (int j = i; j < N; j += K) {
S[j] = ch;
}
}
// Clear the map M
M.clear();
}
// Return the modified string
return S;
}
// Driver Code
int main()
{
string S = "ab??";
int K = 2;
cout << modifyString(S, K);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to modify the given String
// such that String S is a period of K
static String modifyString(char[] S, int K)
{
int N = S.length;
// Iterate over the range [0, K]
for (int i = 0; i < K; i++) {
// Stores the frequency of the
// characters S[i + j*K]
HashMap M = new HashMap<>();
// Iterate over the String with
// increment of K
for (int j = i; j < N; j += K) {
if(M.containsKey(S[j])){
M.put(S[j], M.get(S[j])+1);
}
else{
M.put(S[j], 1);
}
}
// Print "-1"
if (M.size() > 2) {
return "-1";
}
else if (M.size() == 1) {
if (M.get('?') != 0) {
// Replace all characters
// of the String with '?'
// to 'a' to make it smallest
for (int j = i; j < N; j += K) {
S[j] = 'a';
}
}
}
// Otherwise
else if (M.size() == 2) {
char ch=' ';
// Find the character other
// than '?'
for (Map.Entry entry : M.entrySet()) {
if (entry.getKey() != '?') {
ch = entry.getKey();
}
}
// Replace all characters
// of the String with '?'
// to character ch
for (int j = i; j < N; j += K) {
S[j] = ch;
}
}
// Clear the map M
M.clear();
}
// Return the modified String
return String.valueOf(S);
}
// Driver Code
public static void main(String[] args)
{
String S = "ab??";
int K = 2;
System.out.print(modifyString(S.toCharArray(), K));
}
}
// This code is contributed by umadevi9616
Python3
# python 3 program for the above approach
# Function to modify the given string
# such that string S is a period of K
def modifyString(S,K):
N = len(S)
S = list(S)
# Iterate over the range [0, K]
for i in range(K):
# Stores the frequency of the
# characters S[i + j*K]
M = {}
# Iterate over the string with
# increment of K
for j in range(i,N,K):
if S[j] in M:
M[S[j]] += 1
else:
M[S[j]] = 1
# Print "-1"
if (len(M) > 2):
return "-1"
elif (len(M) == 1):
if (M['?'] != 0):
# Replace all characters
# of the string with '?'
# to 'a' to make it smallest
for j in range(i,N,K):
S[j] = 'a'
# Otherwise
elif(len(M) == 2):
ch = ''
# Find the character other
# than '?'
for key,value in M.items():
if (key != '?'):
ch = key
# Replace all characters
# of the string with '?'
# to character ch
for j in range(i,N,K):
S[j] = ch
# Clear the map M
M.clear()
S = ''.join(S)
# Return the modified string
return S
# Driver Code
if __name__ == '__main__':
S = "ab??"
K = 2
print(modifyString(S, K))
# This code is contributed by ipg2016107.
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG {
// Function to modify the given String
// such that String S is a period of K
static String modifyString(char[] S, int K) {
int N = S.Length;
// Iterate over the range [0, K]
for (int i = 0; i < K; i++) {
// Stores the frequency of the
// characters S[i + j*K]
Dictionary M = new Dictionary();
// Iterate over the String with
// increment of K
for (int j = i; j < N; j += K) {
if (M.ContainsKey(S[j])) {
M.Add(S[j], M[S[j]] + 1);
} else {
M.Add(S[j], 1);
}
}
// Print "-1"
if (M.Count > 2) {
return "-1";
} else if (M.Count == 1) {
if (M['?'] != 0) {
// Replace all characters
// of the String with '?'
// to 'a' to make it smallest
for (int j = i; j < N; j += K) {
S[j] = 'a';
}
}
}
// Otherwise
else if (M.Count == 2) {
char ch = ' ';
// Find the character other
// than '?'
foreach (KeyValuePair entry in M) {
if (entry.Key != '?') {
ch = entry.Key;
}
}
// Replace all characters
// of the String with '?'
// to character ch
for (int j = i; j < N; j += K) {
S[j] = ch;
}
}
// Clear the map M
M.Clear();
}
// Return the modified String
return String.Join("",S);
}
// Driver Code
public static void Main(String[] args) {
String S = "ab??";
int K = 2;
Console.Write(modifyString(S.ToCharArray(), K));
}
}
// This code is contributed by umadevi9616
Javascript
abab
时间复杂度: O(N)
辅助空间: O(1)