给定一个由字母组成的字符串S ,使其ASCII值遵循算术级数。任务是找到违反AP的字母和字母索引。另外,用适当的字母替换该字母并打印字符串。
例子:
Input: S = “abcdffghijkl”
Output: 4 -> f
abcdefghijkl
Explanation:
In the given string S, each character having index i varies from its previous index i-1 by one character in the forward direction.
For i = 4, S[4] = ‘f’ and S[3] = ‘d’ whereas S[i] should have been ‘e’ as a result of which it would have obeyed the pattern. Thus replace S[4] with ‘e’. Thus the modified string S is “abcdefghijkl”.
Input: S = “aeimqux”
Output: 6 -> x
aeimquy
Input: S = “beimquy”
Output: 0 -> b
aeimquy
Input: S = “xyzac”
Output: 4 -> c
xyzab
方法:
- 创建一个数组来存储a – z中字母的ASCII值。
- 遍历字符串S,并且发现使用先前创建的阵列字符串的两个连续字符的ASCII值之间的差和在一组将这些值存储。
- 对于集合中的每个值,说D ,构造一个字符串T ,其字符的ASCII值具有D单位的共同差异。
- 每次构造新的字符串T时,其起始字符应与给定字符串S的起始字符相同。
- 如果字符串T与字符串S相差1个字符,则T是必需的修改后的字符串。查找这两个字符串不同的唯一索引。打印该索引及其对应的字符。
- 如果T与S相差超过1个字符,则丢弃字符串T ,然后转到步骤3。
- 如果没有一个重建的字符串与原始字符串相差1个字符,则字符串S的第一个字符是违反AP的字符串。
Explanation of the above approach using an example:
S = “abcdffghijkl”
set = {0, 1, 2}
String reconstructed considering fixed difference as 0 is “aaaaaaaaaaaa”. This string differs from the original string by 11 positions and hence it is not the required string.
String reconstructed considering fixed difference as 1 is “abcdefghijkl”. This string differs from the original string by 1 position and hence it is the required string. The required index where the character has to be modified is 4.
下面是上述方法的实现:
C++
// C++ program for the above problem
#include
using namespace std;
// Function to modify the given
// string and find the index
// where modification is needed
void string_modify(string s)
{
// Array to store the ASCII
// values of alphabets
char alphabets[26];
int flag = 0, hold_i;
char hold_l;
int i;
// loop to compute the ASCII
// values of characters a-z
for (i = 0; i < 26; i++) {
alphabets[i] = i + 'a';
}
// Set to store all the
// possible differences
// between consecutive elements
set difference;
string reconstruct = "";
// Loop to find out the
// differences between
// consecutive elements
// and storing them in the set
for (int i = 1;
i < s.size(); i++) {
difference.insert(s[i] - s[i - 1]);
}
// Checks if any character of the
// string disobeys the pattern
if (difference.size() == 1) {
cout << "No modifications required";
return;
}
// Constructing the strings with
// all possible values of consecutive
// difference and comparing them
// with staring string S.
for (auto it = difference.begin();
it != difference.end(); it++) {
int index = s[0] - 'a';
reconstruct = "";
flag = 0;
for (int i = 0;
i < s.size()
&& flag <= 1;
i++) {
reconstruct += alphabets[index];
index += *it;
if (index < 0) {
index += 26;
}
index %= 26;
if (reconstruct[i] != s[i]) {
flag++;
hold_i = i;
hold_l = s[i];
}
}
if (flag == 1) {
s[hold_i] = reconstruct[hold_i];
break;
}
}
if (flag > 1) {
hold_i = 0;
hold_l = s[0];
int temp = (s[1] - 'a' - (s[2] - s[1])) % 26;
if (temp < 0) {
temp += 26;
}
s[0] = alphabets[temp];
}
cout << hold_i << " -> "
<< hold_l << endl
<< s << endl;
}
// Driver Code
int main()
{
string s = "aeimqux";
string_modify(s);
}
Java
// Java program for the above problem
import java.util.*;
class GFG{
// Function to modify the given
// String and find the index
// where modification is needed
static void string_modify(char[] s)
{
// Array to store the ASCII
// values of alphabets
char []alphabets = new char[26];
int flag = 0, hold_i = 0;
char hold_l = 0;
int i;
// loop to compute the ASCII
// values of characters a-z
for(i = 0; i < 26; i++)
{
alphabets[i] = (char)(i + 'a');
}
// Set to store all the
// possible differences
// between consecutive elements
HashSetdifference = new HashSet();
String reconstruct = "";
// Loop to find out the
// differences between
// consecutive elements
// and storing them in the set
for(i = 1; i < s.length; i++)
{
difference.add(s[i] - s[i - 1]);
}
// Checks if any character of the
// String disobeys the pattern
if (difference.size() == 1)
{
System.out.print("No modifications required");
return;
}
// Constructing the Strings with
// all possible values of consecutive
// difference and comparing them
// with staring String S.
for(int it : difference)
{
int index = s[0] - 'a';
reconstruct = "";
flag = 0;
for(i = 0; i < s.length && flag <= 1; i++)
{
reconstruct += alphabets[index];
index += it;
if (index < 0)
{
index += 26;
}
index %= 26;
if (reconstruct.charAt(i) != s[i])
{
flag++;
hold_i = i;
hold_l = s[i];
}
}
if (flag == 1)
{
s[hold_i] = reconstruct.charAt(hold_i);
break;
}
}
if (flag < 1)
{
hold_i = 0;
hold_l = s[0];
int temp = (s[1] - 'a' - (s[2] - s[1])) % 26;
if (temp < 0)
{
temp += 26;
}
s[0] = alphabets[temp];
}
System.out.print(hold_i + " -> " +
hold_l + "\n" +
String.valueOf(s) + "\n");
}
// Driver Code
public static void main(String[] args)
{
String s = "aeimqux";
string_modify(s.toCharArray());
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above problem
# Function to modify the given
# string and find the index
# where modification is needed
def string_modify(s):
# Array to store the ASCII
# values of alphabets
alphabets = []
flag, hold_i = 0, 0
hold_l = s[0]
# Loop to compute the ASCII
# values of characters a-z
for i in range(26):
alphabets.append(chr(i + ord('a')))
# Set to store all the
# possible differences
# between consecutive elements
difference = set()
reconstruct = ""
# Loop to find out the
# differences between
# consecutive elements
# and storing them in the set
for i in range(1, len(s)):
difference.add(ord(s[i]) -
ord(s[i - 1]))
# Checks if any character of the
# string disobeys the pattern
if (len(difference) == 1):
print("No modifications required")
return
# Constructing the strings with
# all possible values of consecutive
# difference and comparing them
# with staring string S.
for it in difference:
index = ord(s[0]) - ord('a')
reconstruct = ""
flag = 0
i = 0
while ((i < len(s)) and (flag <= 1)):
reconstruct += alphabets[index]
index += it
if (index < 0):
index += 26
index %= 26
if (reconstruct[i] != s[i]):
flag += 1
hold_i = i
hold_l = s[i]
i += 1
if (flag == 1):
s[hold_i] = reconstruct[hold_i]
break
if (flag > 1):
hold_i = 0
hold_l = s[0]
temp = (ord(s[1]) - ord('a') -
(ord(s[2]) - ord(s[1]))) % 26
if (temp < 0):
temp += 26
s[0] = alphabets[temp]
print(hold_i, "->", hold_l)
print("".join(s))
# Driver code
s = list("aeimqux")
string_modify(s)
# This code is contributed by divyeshrabadiya07
C#
// C# program for the above problem
using System;
using System.Collections.Generic;
class GFG{
// Function to modify the given
// String and find the index
// where modification is needed
static void string_modify(char[] s)
{
// Array to store the ASCII
// values of alphabets
char []alphabets = new char[26];
int flag = 0, hold_i = 0;
char hold_l = (char)0;
int i;
// loop to compute the ASCII
// values of characters a-z
for(i = 0; i < 26; i++)
{
alphabets[i] = (char)(i + 'a');
}
// Set to store all the
// possible differences
// between consecutive elements
HashSetdifference = new HashSet();
String reconstruct = "";
// Loop to find out the
// differences between
// consecutive elements
// and storing them in the set
for(i = 1; i < s.Length; i++)
{
difference.Add(s[i] - s[i - 1]);
}
// Checks if any character of the
// String disobeys the pattern
if (difference.Count == 1)
{
Console.Write("No modifications required");
return;
}
// Constructing the Strings with
// all possible values of consecutive
// difference and comparing them
// with staring String S.
foreach(int it in difference)
{
int index = s[0] - 'a';
reconstruct = "";
flag = 0;
for(i = 0; i < s.Length && flag <= 1; i++)
{
reconstruct += alphabets[index];
index += it;
if (index < 0)
{
index += 26;
}
index %= 26;
if (reconstruct[i] != s[i])
{
flag++;
hold_i = i;
hold_l = s[i];
}
}
if (flag == 1)
{
s[hold_i] = reconstruct[hold_i];
break;
}
}
if (flag < 1)
{
hold_i = 0;
hold_l = s[0];
int temp = (s[1] - 'a' - (s[2] - s[1])) % 26;
if (temp < 0)
{
temp += 26;
}
s[0] = alphabets[temp];
}
Console.Write(hold_i + " -> " +
hold_l + "\n" +
String.Join("", s) + "\n");
}
// Driver Code
public static void Main(String[] args)
{
String s = "aeimqux";
string_modify(s.ToCharArray());
}
}
// This code is contributed by Amit Katiyar
6 -> x
aeimquy
时间复杂度: O(N)
辅助空间: O(N)