Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Circular shifts. Application: computational biology. A string s is a circular sh

ID: 3653206 • Letter: C

Question

Circular shifts. Application: computational biology. A string s is a circular shift of a string t if its characters can be circularly shifted to the right by some number of positions, e.g., actgacg is a circular shift of tgacgac, and vice versa. Write a program that checks whether one string s is a circular shift of another t. Hint: it's a one liner with indexOf and string concatenation.

Explanation / Answer

import java.util.Scanner; public class CircularShifts { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter First string"); String s = input.next(); System.out.println("Enter Second string"); String t = input.next(); // first we are adding string 's' to the end of string 's' using concatenation // Now we are checking if the resultant string contains the string 't' as sub-string using String indexOf method. If it contains string 't' then it gives the position of sub-string 't' else it gives -1. if(s.concat(s).indexOf(t) >= 0) { System.out.println("First string is circular shift of second string"); } else System.out.println("First string is not circular shift of second string"); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote