首页 > 其他分享 >力扣---面试题 01.09. 字符串轮转

力扣---面试题 01.09. 字符串轮转

时间:2023-03-29 22:55:45浏览次数:35  
标签:力扣 面试题 return String s2 s1 --- length 字符串

字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。

示例1:

输入:s1 = "waterbottle", s2 = "erbottlewat"
输出:True
示例2:

输入:s1 = "aa", s2 = "aba"
输出:False
提示:

字符串长度在[0, 100000]范围内。
说明:

你能只调用一次检查子串的方法吗?

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/string-rotation-lcci
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。


 

拖了好久的KMP算法,一直拖着没去手撕。这次不打算拖了。

先放两个分别调用库函数和哈希值比较的代码,然后就去手撕KMP。

官解库函数:

class Solution {
    public boolean isFlipedString(String s1, String s2) {
        return s1.length() == s2.length() && (s1 + s1).contains(s2);
    }
}

作者:LeetCode-Solution
链接:https://leetcode.cn/problems/string-rotation-lcci/solution/zi-fu-chuan-lun-zhuan-by-leetcode-soluti-kc8z/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 

 

自己写的哈希值比较:

class Solution {
    public boolean isFlipedString (String s1, String s2) {
        if (s1.length() != s2.length()) {
            return false;
        }
        int len = s1.length();
        if (len == 0) {
            return true;
        }
        char tem = s2.charAt(0);
        int hash1 = s2.hashCode();
        
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) == tem) {
                if ((s1.substring(i, len) + s1.substring(0, i)).hashCode() == hash1) {
                    return true;
                }
            }
        }
        return false;
    }
}

 

 KMP:

 

标签:力扣,面试题,return,String,s2,s1,---,length,字符串
From: https://www.cnblogs.com/allWu/p/17270751.html

相关文章

  • 【THM】Putting it all together(网站组件协同工作介绍)-学习
    本文相关的TryHackMe实验房间链接:https://tryhackme.com/room/puttingitalltogether本文相关内容:了解Web服务的所有单独组件如何协同工作,这种协同工作能让我们可以访问自......
  • push代码时遇到的问题--github-ssh私钥修改
    推送代码到github时遇到:@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@WARNING:REMOTEHOSTIDENTIFICATIONHASCHANGED!@@@@@@@@@@@@@@@@@......
  • linux操作--6
    rpm管理rpm是linux中下载包的打包和安装工具,类似于windos下的setup.exerpm-pa(查看系统目前已安装的包)rpm一些常用查询指令rpm-e(卸载rpm包)安装rpm包YUMyum是一......
  • 【性能优化】Linux内存调试工具-pmap
    简介pmap是一款对进程内存占用率进行分析的Linux环境调试工具,他提供了进程的内存映射,可以用于显示一个或多个进程的内存状态。pmap工具使用说明Usage:pmap[options]P......
  • 堆栈分析-常见服务器问题
    一、常见服务器问题定位:1、常见服务器问题:日常开发中,我们常见的服务器问题主要可以归类为一下几种::CPU过载问题内存过高问题磁盘IO问题网络问题2、服务器问题定位总体思......
  • listening-list-for-children
    萍萍无奇的ListeningListDatetime:2023-03-29T21:18+08:00Categories:Music|Education我有时候思考,我们要怎样教育下一代。这种感觉在看毕设的那些数据时候尤为强......
  • PyQt5学习 (4)--QAbstractButton(下)
    QAbstractBUtton:  所有按钮控件的基类  提供按钮的通用功能  继承自QWidget  属于抽象类别,不能直接去使用,必须借助于子类(除非你觉得子类不够用,想自定义一个按......
  • leetcode-1089-easy
    DuplicateZerosGivenafixed-lengthintegerarrayarr,duplicateeachoccurrenceofzero,shiftingtheremainingelementstotheright.Notethatelementsbe......
  • leetcode-1317-easy
    ConvertIntegertotheSumofTwoNo-ZeroIntegersNo-Zerointegerisapositiveintegerthatdoesnotcontainany0initsdecimalrepresentation.Givenani......
  • leetcode-1009-easy
    ComplementofBase10IntegerThecomplementofanintegeristheintegeryougetwhenyouflipallthe0'sto1'sandallthe1'sto0'sinitsbinaryreprese......