Computer Science, asked by parasathanuja, 4 months ago

write a program that forms a new sentence on taking an input string as per below algorithm the alphabet should be N number of times in forward mode

Answers

Answered by MrsZiddi
7

Given an array of strings (all lowercase letters), the task is to group them in such a way that all strings in a group are shifted versions of each other. Two string S and T are called shifted if,

S.length = T.length

and

S[i] = T[i] + K for

1 <= i <= S.length for a constant integer K

For example strings, {acd, dfg, wyz, yab, mop} are shifted versions of each other.

Input : str[] = {"acd", "dfg", "wyz", "yab", "mop",

"bdfh", "a", "x", "moqs"};

Output : a x

acd dfg wyz yab mop

bdfh moqs

All shifted strings are grouped together.

Answered by anjaliom1122
0

Answer:

From the end (n-1, where n is the length of an array) to the beginning, traverse the Shift array (index 0). Take the modulus of 26 and multiply the value at the previous index by the value at the current index (Total number of characters in English).

Explanation:

The steps to solving this problem are as follows:-

  • This step is carried out to determine the total number of shift operations that must be performed on each character of the input string.
  • Create a character array from the input string.
  • From the beginning (index 0) to the end (index n), traverse the String array (n-1, where n is the length of an array). Apply the shift operation to every single character in an array.
  • Return a modified array of characters as a string.

Program:

public class Main {

   public static void main(String[] args) {        

       Main main = new Main();

       String result = main.shiftingLetters("abcd", new int[] {3, 5, 9, 1});

       System.out.print(result);

   }

   

   /* Solution */

   public String shiftingLetters(String S, int[] shifts) {

       

       int previous = 0;

       for(int i=shifts.length-1; i>=0; i--){            

           shifts[i] = (shifts[i] + previous) % 26;

           previous = shifts[i];

       }

       

       char[] chars = S.toCharArray();

       for(int i=0; i<chars.length; i++){

           chars[i] = (char)('a' + (((int)chars[i] + shifts[i]) % 'a') % 26);

       }

       return String.valueOf(chars);

   }

}

Similar questions