Saturday, December 28, 2024
Google search engine
HomeData Modelling & AIGenerate string by incrementing character of given string by number present at...

Generate string by incrementing character of given string by number present at corresponding index of second string

Given two strings S[] and N[] of the same size, the task is to update string S[] by adding the digit of string N[] of respective indices.

Examples:

Input: S = “sun”, N = “966”
Output: bat

Input: S = “apple”, N = “12580”
Output: brute

Approach: The idea is to traverse the string S[] from left to right. Get the ASCII value of string N[] and add it to the ASCII value of string S[]. If the value exceeds 122, which is the ASCII value of the last alphabet ‘z’. Then subtract the value by 26, which is the total count of English alphabets. Update string S with the character of ASCII value obtained. Follow the steps below to solve the problem:

  • Iterate over the range [0, S.size()) using the variable i and perform the following tasks:
    • Initialize the variables a and b as the integer and ascii value of N[i] and S[i].
    • If b is greater than 122 then subtract 26 from b.
    • Set S[i] as char(b).
  • After performing the above steps, print the value of S[] as the answer.

Below is the implementation of the above approach.

C++




// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to update string
string updateStr(string S, string N)
{
    for (int i = 0; i < S.size(); i++) {
 
        // Get ASCII value
        int a = int(N[i]) - '0';
        int b = int(S[i]) + a;
 
        if (b > 122)
            b -= 26;
 
        S[i] = char(b);
    }
    return S;
}
 
// Driver Code
int main()
{
    string S = "sun";
    string N = "966";
    cout << updateStr(S, N);
    return 0;
}


Java




// Java code to implement above approach
import java.util.*;
public class GFG {
 
  // Function to update string
  static String updateStr(String S, String N)
  {
    String t = "";
    for (int i = 0; i < S.length(); i++) {
 
      // Get ASCII value
      int a = (int)(N.charAt(i) - '0');
      int b = (int)(S.charAt(i) + a);
 
      if (b > 122)
        b -= 26;
 
      char x = (char)b;
      t +=x;
    }
    return t;
  }
 
  // Driver code
  public static void main(String args[])
  {
    String S = "sun";
    String N = "966";
    System.out.println(updateStr(S, N));
 
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Python3




# Python code for the above approach
 
# Function to update string
def updateStr(S, N):
    S = list(S)
    for i in range(len(S)):
 
        # Get ASCII value
        a = ord(N[i]) - ord('0')
        b = ord(S[i]) + a
 
        if (b > 122):
            b -= 26
 
        S[i] = chr(b)
    return "".join(S)
 
# Driver Code
 
S = "sun"
N = "966"
print(updateStr(S, N))
 
# This code is contributed by Saurabh Jaiswal


C#




// C# code to implement above approach
using System;
public class GFG {
 
  // Function to update string
  static String updateStr(String S, String N)
  {
    String t = "";
    for (int i = 0; i < S.Length; i++) {
 
      // Get ASCII value
      int a = (int)(N[i] - '0');
      int b = (int)(S[i] + a);
 
      if (b > 122)
        b -= 26;
 
      char x = (char)b;
      t +=x;
    }
    return t;
  }
 
  // Driver code
  public static void Main(String []args)
  {
    String S = "sun";
    String N = "966";
    Console.WriteLine(updateStr(S, N));
 
  }
}
 
// This code is contributed by shikhasingrajput


Javascript




<script>
   // JavaScript code for the above approach
 
   // Function to update string
   function updateStr(S, N) {
     S = S.split('')
     for (let i = 0; i < S.length; i++) {
 
       // Get ASCII value
       let a = (N[i].charCodeAt(0) - '0'.charCodeAt(0));
       let b = (S[i].charCodeAt(0)) + a;
 
       if (b > 122)
         b -= 26;
 
       S[i] = String.fromCharCode(b);
     }
     return S.join('');
   }
 
   // Driver Code
 
   let S = "sun";
   let N = "966";
   document.write(updateStr(S, N));
 
 // This code is contributed by Potta Lokesh
 </script>


Output

bat

Time Complexity: O(|S|)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments