2.7 Lab: Circular Shift

2.7 Lab: Circular Shift
This lab is an introductory lab to make sure you know how to submit your code. Note that you are welcome to use your own IDE and copy your code into the labs, or to use the code editor that is provided.

A string s is a circular shift (also known as a circular rotation) of a string t if it matches when the characters are circularly shifted by some number of positions; e.g. “ACTGACG” is a circular shift of “TGACGAC” and vice versa. Detecting this condition is important in the study of genomic sequences. Write a Java program that checks if the two command line arguments it is given are circular shifts of one another. Note that the test can be done in one line with a clever use of the built in String methods. Your program should output Yes if they are circular shifts and No if they are not.

import java.util.ArrayList;
        import java.util.List;
        import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String s = cin.next();
        String t = cin.next();
        char[] a = s.toCharArray();
        char[] b = t.toCharArray();
        int count = 0;
        int count1 = 0;
        int count2 = 0;
        List<Integer> index1 = new ArrayList<Integer>();
        if(a.length == b.length){
            for(char c :b){
                if(a[0]==c){
                    for(int n=0;n<b.length;n++){
                        if(a[0]==b[n]){
                            index1.add(n);
                        }
                    }
                    for(int n1 =0; n1<index1.size(); n1++){
                        count1++;
                        int k = index1.get(n1);
                        char[]array  = new char[a.length];
                        for(int n2 = 0; n2<a.length;n2++) {
                            if (k == a.length-1 ) {
                                if(n2 ==a.length-1){
                                    array[n2]=a[0];
                                }
                                else{
                                    array[n2] = a[n2+1];
                                }

                            }
                            else if(k ==1){
                                if(n2 ==0){
                                    array[0]=a[a.length-1];
                                }
                                else{
                                    array[n2] = a[n2-1];
                                }
                            }
                            else {
                                if (k + n2 > a.length) {
                                    array[n2] = a[n2 + k - a.length];
                                } else if (k + n2 == a.length) {
                                    array[n2] = a[0];
                                } else
                                    array[n2] = a[n2 + k];
                            }
                        }
                        for(int n3 = 0; n3<array.length;n3++){
                            if(!(array[n3]==b[n3])){
                                break;
                            }
                            else
                                count2++;
                        }
                        if(count2 == array.length){
                            System.out.println("Yes");
                            count1=-1;
                            break;
                        }
                    }
                    if(count1 == index1.size()){
                        System.out.println("No");
                    }

                }
                else count++;
                if(count == b.length){
                    System.out.println("No");
                }
            }
        }else{
            System.out.println("No");
        }

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值