📜  通过改变字符使AP字符串

📅  最后修改于: 2021-09-08 12:41:59             🧑  作者: Mango

给定一个由字母组成的字符串S ,它们的 ASCII 值遵循算术级数。任务是找到不服从 AP 的字母和字母索引。此外,用适当的字母替换此字母并打印字符串。

例子:

方法:

  1. 创建一个数组来存储a – z中字母的 ASCII 值。
  2. 遍历字符串S,并且发现使用先前创建的阵列字符串的两个连续字符的ASCII值之间的差和在一组将这些值存储。
  3. 对于集合中的每个值D ,构造一个字符串T ,其字符在其 ASCII 值中具有D单位的共同差异。
  4. 每次构造新字符串T 时,其起始字符应与给定字符串S的起始字符相同。
  5. 如果字符串T与字符串S相差 1 个字符,则T是所需的修改字符串。找到这两个字符串不同的唯一索引。打印该索引及其对应的字符。
  6. 如果TS相差超过 1 个字符,则丢弃字符串T并转到步骤 3。
  7. 如果重建的字符串没有一个与原始字符串相差 1 个字符,那么字符串S的第一个字符就是不服从 AP 的字符。

下面是上述方法的实现:

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)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live