Determines whether the two strings are mutually rotating words

Source: Internet
Author: User

Topic

For a string str, the string that is moved from the front to the back is called the spin word of Str. For example str= "12345", its rotational words have "23451", "34512", "45123", "51234". Given two strings A and B, determine whether A and B are mutually rotating words.

Realize

This is a very flattering implementation, adding the original string one at a time, and using a string-contains method to determine whether to include the matched string as judged

import org.junit.Assert;import org.junit.Test;/** * @author lorem */public class IsRotationTest {    boolean isRotation(String str1, String str2) {        if (str1 == null || str2 == null || str1.length() != str2.length()) {            return false;        }        String str = str1 + str1;        if (str.contains(str2)) {            return true;        }        return false;    }    @Test    public void test() {        String str1 = "abcd";        String str2 = "dcad";        Assert.assertEquals(true,isRotation(str1, str2));    }}

Determines whether the two strings are mutually rotating words

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.