📌  相关文章
📜  通过增加每个字符到词尾的距离来修改字符串

📅  最后修改于: 2021-09-06 05:10:34             🧑  作者: Mango

给定一个字符串S ,任务是通过将每个字符S[i]替换为一个新字符来修改给定的字符串,该字符的值是( S[i] + 从词尾开始的位置)

例子:

方法:想法是将给定的字符串拆分为单词并单独修改每个单词。以下是步骤:

  1. 首先,将给定的字符串S标记为单个单词。
  2. 迭代单词并为单词中的每个字符添加从末尾到它的位置。
  3. 然后,将结果单词添加到最终字符串,例如res
  4. 不断重复以上两个步骤,直到字符串的每个单词都被转换。

以下是上述方法的程序:

C++
// C++ implementation of
// the above approach
#include 
using namespace std;
 
// Function to transform and return
// the transformed word
string util(string sub)
{
    int n = sub.length();
    int i = 0;
 
    // Stores resulting word
    string ret = "";
 
    // Iterate over the word
    while (i < n)
    {
         
        // Add the position
        // value to the letter
        int t = (sub[i] - 'a') +
                  n - 1 - i;
 
        // Convert it back to character
        char ch = (char)(t % 26 + 97);
 
        // Add it to the string
        ret = ret + ch;
 
        i++;
    }
    return ret;
}
 
// Function to transform the
// given string
void manipulate(string s)
{
     
    // Size of string
    int n = s.length();
    int i = 0, j = 0;
 
    // Stores resultant string
    string res = "";
 
    // Iterate over given string
    while (i < n)
    {
 
        // End of word is reached
        if (s[i] == ' ')
        {
 
            // Append the word
            res += util(s.substr(j, i));
            res = res + " ";
            j = i + 1;
            i = j + 1;
        }
        else
        {
            i++;
        }
    }
 
    // For the last word
    res = res + util(s.substr(j, i));
 
    cout << res << endl;
}
 
// Driver code
int main()
{
     
    // Given string
    string s = "acm fkz";
 
    // Function call
    manipulate(s);
 
    return 0;
}
 
// This code is contributed by divyeshrabadiya07


Java
// Java implementation of
// the above approach
 
import java.util.*;
import java.lang.*;
import java.io.*;
 
class GFG {
 
    // Function to transform the given string
    public static void manipulate(String s)
    {
        // Size of string
        int n = s.length();
        int i = 0, j = 0;
 
        // Stores resultant string
        String res = "";
 
        // Iterate over given string
        while (i < n) {
 
            // End of word is reached
            if (s.charAt(i) == ' ') {
 
                // Append the word
                res += util(s.substring(j, i));
                res = res + " ";
                j = i + 1;
                i = j + 1;
            }
 
            else {
                i++;
            }
        }
 
        // For the last word
        res = res + util(s.substring(j, i));
 
        System.out.println(res);
    }
 
    // Function to transform and return
    // the transformed word
    public static String util(String sub)
    {
        int n = sub.length();
        int i = 0;
 
        // Stores resulting word
        String ret = "";
 
        // Iterate over the word
        while (i < n) {
 
            // Add the position
            // value to the letter
            int t = (sub.charAt(i) - 'a')
                    + n - 1 - i;
 
            // Convert it back to character
            char ch = (char)(t % 26 + 97);
 
            // Add it to the string
            ret = ret + String.valueOf(ch);
 
            i++;
        }
 
        return ret;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        // Given string
        String s = "acm fkz";
 
        // Function Call
        manipulate(s);
    }
}


Python3
# Python3 implementation of
# the above approach
 
# Function to transform and return
# the transformed word
def util(sub):
     
    n = len(sub)
    i = 0
 
    # Stores resulting word
    ret = ""
 
    # Iterate over the word
    while i < n:
 
        # Add the position
        # value to the letter
        t = (ord(sub[i]) - 97) + n - 1 - i
 
        # Convert it back to character
        ch = chr(t % 26 + 97)
 
        # Add it to the string
        ret = ret + ch
 
        i = i + 1
 
    return ret
 
# Function to transform the
# given string
def manipulate(s):
 
    # Size of string
    n = len(s)
    i = 0
    j = 0
 
    # Stores resultant string
    res = ""
 
    # Iterate over given string
    while i < n:
 
        # End of word is reached
        if s[i] == ' ':
 
            # print(s[j:j+i])
            # Append the word
            res += util(s[j : j + i])
            res = res + " "
            j = i + 1
            i = j + 1
        else:
            i = i + 1
 
    # For the last word
    res = res + util(s[j : j + i])
 
    print(res)
 
# Driver code
if __name__ == "__main__":
     
    # Given string
    s = "acm fkz"
 
    # Function call
    manipulate(s)
 
# This code is contributed by akhilsaini


C#
// C# implementation of
// the above approach
using System;
class GFG{
 
// Function to transform the given string
public static void manipulate(String s)
{
  // Size of string
  int n = s.Length;
  int i = 0, j = 0;
 
  // Stores resultant string
  String res = "";
 
  // Iterate over given string
  while (i < n)
  {
    // End of word is reached
    if (s[i] == ' ')
    {
      // Append the word
      res += util(s.Substring(j, i - j));
      res = res + " ";
      j = i + 1;
      i = j + 1;
    }
    else
    {
      i++;
    }
  }
 
  // For the last word
  res = res + util(s.Substring(j, i - j));
 
  Console.WriteLine(res);
}
 
// Function to transform and return
// the transformed word
public static String util(String sub)
{
  int n = sub.Length;
  int i = 0;
 
  // Stores resulting word
  String ret = "";
 
  // Iterate over the word
  while (i < n)
  {
    // Add the position
    // value to the letter
    int t = (sub[i] - 'a') +
             n - 1 - i;
 
    // Convert it back to character
    char ch = (char)(t % 26 + 97);
 
    // Add it to the string
    ret = ret + String.Join("", ch);
 
    i++;
  }
 
  return ret;
}
 
// Driver Code
public static void Main(String[] args)
{
  // Given string
  String s = "acm fkz";
 
  // Function Call
  manipulate(s);
}
}
 
// This code is contributed by shikhasingrajput


输出:
cdm hlz



时间复杂度: O(N)
辅助空间: O(N)

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