LeetCode面试题 01.09 字符串轮转

题目

解答

java 复制代码
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)

测试用例

java 复制代码
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"));
    }
}

理解并掌握StringstartsWithsubstring的使用方法。

相关推荐
014-code2 小时前
订单超时取消与库存回滚的完整实现(延迟任务 + 状态机)
java·开发语言
java1234_小锋3 小时前
Java高频面试题:Springboot的自动配置原理?
java·spring boot·面试
末央&4 小时前
【天机论坛】项目环境搭建和数据库设计
java·数据库
枫叶落雨2224 小时前
ShardingSphere 介绍
java
花花鱼4 小时前
Spring Security 与 Spring MVC
java·spring·mvc
小白菜又菜5 小时前
Leetcode 2075. Decode the Slanted Ciphertext
算法·leetcode·职场和发展
言慢行善5 小时前
sqlserver模糊查询问题
java·数据库·sqlserver
专吃海绵宝宝菠萝屋的派大星5 小时前
使用Dify对接自己开发的mcp
java·服务器·前端
大数据新鸟6 小时前
操作系统之虚拟内存
java·服务器·网络
Tong Z6 小时前
常见的限流算法和实现原理
java·开发语言