Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc", 3] ="abcabcabc".

On the other hand, we define that string s1 can be obtained from string s2 if we can remove some characters from s2 such that it becomes s1. For example, “abc” can be obtained from “abdbec” based on our definition, but it can not be obtained from “acbbe”.

You are given two non-empty strings s1 and s2 (each at most 100 characters long) and two integers 0 ≤ n1 ≤ 106 and 1 ≤ n2 ≤ 106. Now consider the strings S1 and S2, where S1=[s1,n1] and S2=[s2,n2]. Find the maximum integer M such that [S2,M] can be obtained from S1.

Example:

Input:
s1="acb", n1=4
s2="ab", n2=2 Return:
2

目前只想出了Brute Force做法(1165ms), 看到有<20ms的做法,未深究:

 public class Solution {
public int getMaxRepetitions(String s1, int n1, String s2, int n2) {
char[] arr1 = s1.toCharArray();
char[] arr2 = s2.toCharArray();
int i1 = 0, i2 = 0; // current position
int count1 = 0, count2 = 0; //# of times pass through s1/s2's end while (count1 < n1) {
if (arr1[i1] == arr2[i2]) {
i2++;
if (i2 == arr2.length) {
i2 = 0;
count2++;
}
}
i1++;
if (i1 == arr1.length) {
i1 = 0;
count1++;
}
}
return count2/n2;
}
}

Leetcode: Count The Repetitions的更多相关文章

  1. [LeetCode] Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  2. CH5702 Count The Repetitions

    题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...

  3. 【leetcode 字符串】466. Count The Repetitions

    https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...

  4. 第七周 Leetcode 466. Count The Repetitions 倍增DP (HARD)

    Leetcode 466 直接给出DP方程 dp[i][k]=dp[i][k-1]+dp[(i+dp[i][k-1])%len1][k-1]; dp[i][k]表示从字符串s1的第i位开始匹配2^k个 ...

  5. [LeetCode] 466. Count The Repetitions 计数重复个数

    Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...

  6. [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数

    Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...

  7. [LeetCode] Count of Range Sum 区间和计数

    Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...

  8. [LeetCode] Count of Smaller Numbers After Self 计算后面较小数字的个数

    You are given an integer array nums and you have to return a new counts array. The counts array has ...

  9. [LeetCode] Count Univalue Subtrees 计数相同值子树的个数

    Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...

随机推荐

  1. [工作中的设计模式]解释器模式模式Interpreter

    一.模式解析 解释器模式是类的行为模式.给定一个语言之后,解释器模式可以定义出其文法的一种表示,并同时提供一个解释器.客户端可以使用这个解释器来解释这个语言中的句子. 以上是解释器模式的类图,事实上我 ...

  2. Salt安装(yum不可用时)

        salt-master安装   [salt-master]# yum install salt-master   或者 curl -L http://bootstrap.saltstack.o ...

  3. codeforces泛做..

    前面说点什么.. 为了完成日常积累,傻逼呵呵的我决定来一发codeforces 挑水题 泛做.. 嗯对,就是泛做.. 主要就是把codeforces Div.1的ABCD都尝试一下吧0.0.. 挖坑0 ...

  4. HDU4609 & FFT

    关于这道题请移步kuangbin爷的blog:http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 感觉我一辈子也不能写出这么 ...

  5. trie字典树详解及应用

    原文链接    http://www.cnblogs.com/freewater/archive/2012/09/11/2680480.html Trie树详解及其应用   一.知识简介        ...

  6. Browsersync + Gulp.js

    1.安装 Browsersync 和 依赖包 Gulp npm install browser-sync gulp --save-dev 2.gulpfile.js var gulp = requir ...

  7. Self-introduction (自我介绍)

     * 姓名,本名不想这样就暴露,Rachel我英文名,不愿意叫算了,直接叫我米蟲就好了.   * 性格, 偏执一些,表里不一,表面和善,骨子倔强,我这一生都在追求高逼格,从未间断过 偶尔像个小疯子,有 ...

  8. vim 长句子中的上下移动

    当一个句子很长的时候,屏幕显示不下,就会分为多行,这个时候,你又想找到中间几行某部分的字母,怎么办?这个时候,先按下一个 g ,在按下 j / k ,就可以实现长句子的上下移动了.

  9. Hadoop openssl false

    错误如图 检查Hadoop native 经过: 1. 重新编译cdh的hadoop2.5.0,复制native 2. 重新格式化namenode 都不行,另外openssl和openssl-dev都 ...

  10. WAF攻防研究之四个层次Bypass WAF

    从架构.资源.协议和规则4个层次研究绕过WAF的技术,助于全方位提升WAF防御能力. 绕过WAF的相关技术研究是WAF攻防研究非常重要的一部分,也是最有趣的部分,所以我在写WAF攻防时先写攻击部分.还 ...