Modify string by increasing each character by its distance from the end of the word
Given a string S, the task is to modify the given string by replacing every character S[i] by a new character whose value is (S[i] + its position from the end of the word)
Examples:
Input: S = “acm fkz”
Output: “cdm hlz”
Explanation:
There are 2 words in the given string {“acm”, “fkz”}
For “acm”:
a becomes ‘a’ + 2 = ‘c’
c becomes ‘c’ + 1 = ‘d’
m becomes ‘m’ + 0 = ‘m’
“acm” becomes “cdm”.
Similarly, “fkz” becomes “hlz”.
Therefore, the required answer is “cdm hlz”Input: “geeks for geeks”
Output: “khgls hpr khgls”
Approach: The idea is to split the given string into words and modify each word individually. Below are the steps:
- First, tokenize the given string S into individual words.
- Iterate over words and for each character in a word, add its position from the end to it.
- Then, add the resultant word to the final string, say res.
- Keep repeating the above two steps until every word in the string is transformed.
Below is the program for the above approach:
C++
// C++ implementation of // the above approach #include <bits/stdc++.h> 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 |
Javascript
// Javascript implementation of // the above approach // Function to transform and return // the transformed word function util(sub) { var n = sub.length; var i = 0; // Stores resulting word var ret = "" ; // Iterate over the word while (i < n) { // Add the position // value to the letter var t = (sub[i] - 'a' ) + n - 1 - i; // Convert it back to character var ch = (char)(t % 26 + 97); // Add it to the string ret = ret + ch; i++; } return ret; } // Function to transform the // given string function manipulate(s) { // Size of string var n = s.length(); var i = 0, j = 0; // Stores resultant string var 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)); res = res + " " ; j = i + 1; i = j + 1; } else { i++; } } // For the last word res = res + util(s.substring(j, i)); console.log( "res|n" ); } // Driver code // Given string var s = "acm fkz" ; // Function call manipulate(s); // This code is contributed by Abhijeet Kumar(abhijeet19403) |
Output:
cdm hlz
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...