马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
题目
解答
- package leetcode.editor.cn;
- //leetcode submit region begin(Prohibit modification and deletion)
- class Solution {
- public boolean isFlipedString(String s1, String s2) {
- if (s1 == null && s2 == null) {
- return true;
- }
- if (s1 == null || s2 == null) {
- return false;
- }
- if (s1.isEmpty() && s2.isEmpty()) {
- return true;
- }
- if (s1.length() != s2.length()) {
- return false;
- }
- for (int i = 0, length = s1.length(); i < length; ++i) {
- String ss1 = s1.substring(i);
- if (s2.startsWith(ss1)) {
- if (s2.equals(s1.substring(i, s1.length()) + s1.substring(0, i))) {
- return true;
- }
- }
- }
- return false;
- }
- }
- //leetcode submit region end(Prohibit modification and deletion)
复制代码 测试用例
- package leetcode.editor.cn;
- import org.junit.Assert;
- import org.junit.Before;
- import org.junit.Test;
- public class SolutionTest {
- private Solution s = null;
- @Before
- public void setUp() throws Exception {
- s = new Solution();
- }
- @Test
- public void test1() {
- Assert.assertTrue(s.isFlipedString("waterbottle", "erbottlewat"));
- Assert.assertTrue(s.isFlipedString("erbottlewat", "waterbottle"));
- }
- @Test
- public void test2() {
- Assert.assertFalse(s.isFlipedString("aa", "aba"));
- Assert.assertFalse(s.isFlipedString("aba", "aa"));
- }
- @Test
- public void test3() {
- Assert.assertTrue(s.isFlipedString("waterbottle", "waterbottle"));
- }
- @Test
- public void test4() {
- Assert.assertFalse(s.isFlipedString("abcd", "acdb"));
- }
- }
复制代码 理解并把握String的startsWith和substring的利用方法。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
|