首页 > 其他分享 >leetcode-459-easy

leetcode-459-easy

时间:2023-01-03 21:44:59浏览次数:36  
标签:459 easy s2 s1 substring str 字符串 Input leetcode

Repeated Substring Pattern

Given a string s, check if it can be constructed by taking a substring of it and appending multiple copies of the substring together.

Example 1:

Input: s = "abab"
Output: true
Explanation: It is the substring "ab" twice.
Example 2:

Input: s = "aba"
Output: false
Example 3:

Input: s = "abcabcabcabc"
Output: true
Explanation: It is the substring "abc" four times or the substring "abcabc" twice.
Constraints:

1 <= s.length <= 104
s consists of lowercase English letters.

思路一:长度为 n 的字符串,获取字符串的子字符串,长度从[1, n/2],然后从这个子字符串构造完整的字符串,最后判断两者是否匹配。

    public boolean repeatedSubstringPattern(String s) {
        for (int i = 1; i <= s.length() / 2; i++) {
            String subStr = s.substring(0, i);
            if (repeatedStringMatch(subStr, s)) {
                return true;
            }
        }

        return false;
    }

    public static boolean repeatedStringMatch(String subStr, String s) {
        int subLength = subStr.length();
        int strLength = s.length();

        if (strLength % subLength != 0) {
            return false;
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= strLength / subLength; i++) {
            sb.append(subStr);
        }

        return sb.toString().equals(s);
    }

思路二:看了官方题解,发现有一种更巧妙的解题思路

假设字符串s是由s1+s2组成的,s+s后,str就变成了s1+s2+s1+s2,去掉首尾,破环了首尾的s1和s2,变成了s3+s2+s1+s4,此时str中间就是s2+s1,如果s是循环字串,也就是s1=s2,所以str中间的s2+s1就和原字符串相等。如果s不是循环字串,s1!=s2,那么s1+s2是不等于s2+s1的,也就是str中间不包含s
        String str = s + s;
        return str.substring(1, str.length() - 1).contains(s);

标签:459,easy,s2,s1,substring,str,字符串,Input,leetcode
From: https://www.cnblogs.com/iyiluo/p/17023453.html

相关文章

  • leetcode-492-easy
    ConstructtheRectangleAwebdeveloperneedstoknowhowtodesignawebpage'ssize.So,givenaspecificrectangularwebpage’sarea,yourjobbynowisto......
  • leetcode-144-easy
    BinaryTreePreorderTraversalGiventherootofabinarytree,returnthepreordertraversalofitsnodes'values.Example1:Input:root=[1,null,2,3]Out......
  • [Leetcode Weekly Contest]326
    链接:LeetCode[Leetcode]2520.统计能整除数字的位数给你一个整数num,返回num中能整除num的数位的数目。如果满足nums%val==0,则认为整数val可以整除nums......
  • 【队列】LeetCode 232. 用栈实现队列
    题目链接232.用栈实现队列思路设置一个主栈mainStack和一个辅助栈assistantStack,在进行入队的时候,将mainStack中的元素全部放入assistantStack中,再将x入队,然......
  • [LeetCode] 1325. Delete Leaves With a Given Value 删除给定值的叶子结点
    Givenabinarytree root andaninteger target,deleteallthe leafnodes withvalue target.Notethatonceyoudeletealeafnodewithvalue target, ......
  • 【队列】LeetCode 225. 用队列实现栈
    题目链接225.用队列实现栈思路设置一个主队列mainQueue和一个辅助队列assistantQueue,在进行压栈的时候,将mainQueue中的元素全部放入assistantQueue中,再将x压......
  • Leetcode[LeetCode]4 两个有序数组的中位数
    上图为剑指Offer之字符串的排列,基于回溯法的思想。简单算法01数组中第二大的数02合并排序链表03链表反转04判断链表环05两个链表的首个交点06数组中出现大与一般的数07手写......
  • 【链表】LeetCode 328. 奇偶链表
    题目链接328.奇偶链表思路根据题意,我们只需要将扫描到的索引为奇数的结点不断插入到正确位置。比如1->2->3->4->5==》1->3->2->4->5==》1->3->5->2->4在扫描过......
  • 阿里easyexcel解析百万级大数据量的Excel表格,看这一篇文章就够了
    ​1、应用场景1.1、实际工作中可能会遇到百万条数据量的Excel表格上传到后台进行解析。那么传统的POI,它只适用于数据量较小的Excel表格解析,当数据量到一定级别时,后台程序就......
  • [LeetCode]014-最长公共前缀
    >>>传送门题目编写一个函数来查找字符串数组中的最长公共前缀。如果不存在公共前缀,返回空字符串""。示例示例1输入:strs=["flower","flow","flight"]输出:"fl"......