程序员面试金典 面试题 01.09.字符串轮转

面试题 01.09.字符串轮转

题目描述

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

示例1

输入:s1 = “waterbottle”, s2 = “erbottlewat”
输出:True

示例2

输入:s1 = “aa”, s2 = “aba”
输出:False

提示

  1. 字符串长度在[0, 100000]范围内。
  2. 你能只调用一次检查子串的方法吗?
  3. 如果一个字符串是另一个字符串的旋转,那么它就是在某个特定点上的旋转。例如,字符串waterbottle在3处的旋转意味着在第三个字符处切割waterbottle,并在左半部分(wat)之前放置右半部分(erbottle)。
  4. 本质上,我们是在寻找是否有一种方式可以把第一个字符串分成两部分,即x和y,如此一来,第一个字符串就是xy,第二个字符串就是yx。例如,x = wat,y = erbottle。那么,第一个字符串xy = waterbottle,第二个字符串yx = erbottlewat。
  5. 想想前面的提示。再想想当你将erbottlewat与它本身连接会发生什么。你得到了erbottlewaterbottlewat。

示例代码
C++ sort后对比

#include <bits/stdc++.h>
using namespace std;

class Solution {
public:
    bool isFlipedString(string s1, string s2)
    {
        sort(s1.begin(),s1.end());
        sort(s2.begin(),s2.end());
        return s1 == s2 ? true : false;
    }
};

int main()
{
 	Solution s;
  	string str1_1 = "waterbottle";
  	string str1_2 = "erbottlewat";
  	cout << s.isFlipedString(str1_1,str1_2) << endl;

  	string str2_1 = "aa";
  	string str2_2 = "aba";
  	cout << s.isFlipedString(str2_1,str2_2) << endl;
}

C++ 提交结果
执行用时:16 ms, 在所有 C++ 提交中击败了12.47% 的用户
内存消耗:7.4 MB, 在所有 C++ 提交中击败了97.83% 的用户

示例代码
Java 寻找旋转点后StringBuilder拼接对比

public class Question01_09 {
    public static void main(String[] args) {
        Solution s = new Solution();
        String s1_1 = "waterbottle";
        String s1_2 = "erbottlewat";
        System.out.println(s.isFlipedString(s1_1,s1_2));
        String s2_1 = "aa";
        String s2_2 = "aba";
        System.out.println(s.isFlipedString(s2_1,s2_2));
    }
}

class Solution {
    public boolean isFlipedString(String s1, String s2) {
        if (s1 == null || s2 == null)
            return false;
        if (s1.equals(s2))
            return true;
        int len1 = s1.length();
        int len2 = s2.length();
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (s2.indexOf(s1.substring(0, i + 1)) != -1) {
            i++;
        }
        sb.append(s1.substring(i, len1));
        sb.append(s1.substring(0, i));
        return sb.toString().equals(s2);
    }
}

Java 提交结果
执行用时:133 ms, 在所有 Java 提交中击败了5.02% 的用户
内存消耗:38.5 MB, 在所有 Java 提交中击败了18.83% 的用户

示例代码
Java String方法 一行代码检查长度是否相等,两个s1组合后是否包含s2

public class Question01_09 {
    public static void main(String[] args) {
        Solution s = new Solution();
        String s1_1 = "waterbottle";
        String s1_2 = "erbottlewat";
        System.out.println(s.isFlipedString(s1_1,s1_2));
        String s2_1 = "aa";
        String s2_2 = "aba";
        System.out.println(s.isFlipedString(s2_1,s2_2));
    }
}

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

Java 提交结果
执行用时:0 ms, 在所有 Java 提交中击败了100.00% 的用户
内存消耗:38.2 MB, 在所有 Java 提交中击败了67.14% 的用户

有时候偷懒不一定是差的解决方式
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

W.Lionel.Esaka

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值