TCS Digital Capability Assessment - Ques 1 - 19th march 2021

TCS Digital Capability Assessment - Ques 1 - 19th march 2021

Consider one string as input. You have to check whether the strings obtained from the input string with single backward and single forward shift are the same or not. If they are the same, then print 1; otherwise, print 0.


Note:

Backward Shift : For example, "coding" becomes "odingc" after one backward shift. 

A single circular rotation of the string in which the first character becomes the last character and all the other characters are shifted one index to the left. 


Forward Shift: For example, "coding" becomes "gcodin" after one forward shift.

A single circular rotation of the string in which the last character becomes the first character and all the other characters are shifted to the right. 


Instructions:

The system does not allow any kind of hard coded input value/values. 

The written program code by the candidate will be verified against the inputs that are supplied from the system.

For more clarification, please read the following points carefully till the end.


Example 1:

Input :

sfdlmnop


Output:

0


Example 2:

Input:

mama


Output:

1


Explanation:


In the first example, the string is “sfdlmnop".

Forward Shift: fdlmnops 

Backward Shift: psfdlmnop

Both the strings above are not equal, so the output is 0.


In the second example, the string is "mama"

Forward Shift: amam

Backward Shift: amam

Both the strings above are equal, so the output is 1.


Code:

import java.util.Scanner;

public class MatchingShiftString {
	
	public static void main(String args[]) {
		
		Scanner sc = new Scanner(System.in);

		String str = sc.next().trim();

		String forwardShiftedString = str.substring(1) + str.charAt(0);
		String backwardShiftedString = "" + str.charAt(str.length() - 1) + str.subSequence(0, str.length() - 1);

		if (forwardShiftedString.equals(backwardShiftedString)) {
			System.out.println(1);
		} else {
			System.out.println(0);
		}
	}
}
For More TCS Digital Capability Assessment solution - Click here  or Here

This Blog is Hard work !

Can you give me a treat 😌

Post a Comment

0 Comments