459. Repeated Substring Pattern

Easy

Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.

Example 1:

Input: "abab"
Output: True
Explanation: It's the substring "ab" twice.

Example 2:

Input: "aba"
Output: False

Example 3:

Input: "abcabcabcabc"
Output: True
Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
package leetcode.easy;

public class RepeatedSubstringPattern {
public boolean repeatedSubstringPattern(String s) {
// corner case
if (s == null || s.length() == 0) {
return true;
}
int n = s.length();
for (int len = n / 2; len >= 1; len--) {
if (n % len != 0) {
continue; // s length must can be divided by the pattern length
} else {
String pattern = s.substring(0, len); // pattern string
int i = len; // start index of 2nd pattern
int j = i + len - 1; // end index of 2nd pattern
while (j < n) {
String substr = s.substring(i, j + 1);
if (!pattern.equals(substr)) {
break; // failed for this pattern, try next pattern
} else {
i += len;
j += len;
}
}
// if it past the last substring check, i will be n
if (i == n) {
return true;
}
}
}
return false;
} @org.junit.Test
public void test() {
System.out.println(repeatedSubstringPattern("abab"));
System.out.println(repeatedSubstringPattern("aba"));
System.out.println(repeatedSubstringPattern("abcabcabcabc"));
}
}

LeetCode_459. Repeated Substring Pattern的更多相关文章

  1. 43. leetcode 459. Repeated Substring Pattern

    459. Repeated Substring Pattern Given a non-empty string check if it can be constructed by taking a ...

  2. 459. Repeated Substring Pattern【easy】

    459. Repeated Substring Pattern[easy] Given a non-empty string check if it can be constructed by tak ...

  3. LeetCode 459. 重复的子字符串(Repeated Substring Pattern)

    459. 重复的子字符串 459. Repeated Substring Pattern 题目描述 给定一个非空的字符串,判断它是否可以由它的一个子串重复多次构成.给定的字符串只含有小写英文字母,并且 ...

  4. *459. Repeated Substring Pattern (O(n^2)) two pointers could be better?

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  5. 459. Repeated Substring Pattern

    https://leetcode.com/problems/repeated-substring-pattern/#/description Given a non-empty string chec ...

  6. [LeetCode] Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  7. Repeated Substring Pattern Leetcode

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

  8. LeetCode算法题-Repeated Substring Pattern(Java实现)

    这是悦乐书的第236次更新,第249篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第103题(顺位题号是459).给定非空字符串检查是否可以通过获取它的子字符串并将子字符 ...

  9. [LeetCode] 459. Repeated Substring Pattern 重复子字符串模式

    Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...

随机推荐

  1. 《剑指Offer》-005 -用两个栈实现队列

    如题 (总结要点) 用两个栈实现队列 栈; 先进后出 队列: 先进先出 两个栈, 相等于两个杯子; 把一本水倒来倒去, 取到杯子底部的元素,并且删除,再倒回去 原文链接 : 借鉴学习文章列表 链接1: ...

  2. 【转】Linux虚拟网络基础——tap

    原文:https://blog.csdn.net/chengqiuming/article/details/80071073 ------------------------------------- ...

  3. Linux—— 报错汇总

    前言 记录Linux相关的错误问题和解决方法 问题 tar: Error is not recoverable: exiting now [报错] tar -zxvf mysql-server_5.6 ...

  4. 珠峰培训node 珠峰爬虫| cron 定时任务

    1.cron 定时任务 CronJob var CronJob = require('cron').CronJob; // 秒 分钟 时 天

  5. Sam小结和模板

    Sam 的一些总结 注意在子串在某个节点的性质,其 father 上也会有相同的性质 1. 统计子串出现的次数 在 \(parent\) 树上做 \(dp\),对于每一个节点,初始化为 \(dp[i] ...

  6. 苹果MAC OS查看MAC地址及修改ip

    一,查看mac地址 第一步: 第二步: 第三步: 二,更改IP 第一步: 第二步: 第三步: 最后点击“好”就完成了 我要这天再遮不住我眼,要这地再埋不了我心.要这天下众生都明白我意,要那诸佛都烟消云 ...

  7. ABP 00 常用知识

    1.更改本地预览的端口: 文件路径:\src\ContractMS.Web.Mvc\Properties\launchSettings.json 改这里:"applicationUrl&qu ...

  8. 【09NOIP提高组】Hankson 的趣味题(信息学奥赛一本通 1856)(洛谷 1072)

    题目描述 Hanks 博士是BT (Bio-Tech,生物技术) 领域的知名专家,他的儿子名叫Hankson.现在,刚刚放学回家的Hankson 正在思考一个有趣的问题.今天在课堂上,老师讲解了如何求 ...

  9. Swagger-BootStrap-UI生成的接口文档如何加Basic校验

    首先我们来看看swagger-bootstrap-ui的效果,如图所示: 看起来是不是比Swagger要大气的多. 回到重点上,为什么要给接口文档加密呢? 只对内开放,不对外开放,防止被第三方非公司人 ...

  10. python发送钉钉机器人脚本

    #!/usr/bin/python# -*- coding: utf-8 -*-import requestsimport jsonimport sysimport os headers = {'Co ...