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. 玩转GIT

    远程代码覆盖本地 解决方法: 1.如果还没有 commit 的话,可以用 git checkout . 这样将使所有代码还原到最后一次 commit 的状态 2.如果已经 commit 了,最简单的方 ...

  2. Employment Planning[HDU1158]

    Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...

  3. [MySQL]使用Begin...End语句的一个坑

    写一个触发器,执行单条语句是OK的. 想执行多条语句,尝试在代码中加入BEGIN END.但一加BEGIN END就报错, 错误信息也很诡异,只说某一行出错了,不符合Mysql的语句规范,提示信息就个 ...

  4. 转OSGchina中,array老大的名词解释

    转OSGchina中,array老大的名词解释 转自:http://ydwcowboy.blog.163.com/blog/static/25849015200983518395/ osg:: Cle ...

  5. arp欺骗技术

      ARP欺骗技术-获取内网目标IP访问图片!   简介: ARP(Address Resolution Protocol,地址解析协议)是一个位于TCP/IP协议栈中的网络层,负责将某个IP地址解析 ...

  6. 20145205 《Java程序设计》第7周学习总结

    教材学习内容总结 认识时间与日期 1.格林威治时间(GMT):通过观察太阳而得,因为地球公转轨道为椭圆形且速度不一,本身自传减速而造成误差. 2.世界时(UT):通过观测远方星体跨过子午线而得,受地球 ...

  7. 基于小波变换的数字图像处理(MATLAB源代码)

    基于小波变换的数字图像处理(MATLAB源代码) clear all; close all; clc;M=256;%原图像长度N=64; %水印长度[filename1,pathname]=uiget ...

  8. IOS网络第二天 - 04-黑酷-GDataXML 解析

    ****** - (void)viewDidLoad { [super viewDidLoad]; /** 加载服务器最新的视频信息 */ // 1.创建URL NSURL *url = HMUrl( ...

  9. PHP性能监测的工具介绍 - XHProf

    http://segmentfault.com/a/1190000003509917 http://www.cnblogs.com/casatwy/archive/2013/01/17/2865241 ...

  10. C fread

    fread是一个函数.从一个文件流中读数据,最多读取count个元素,每个元素size字节,如果调用成功返回实际读取到的元素个数,如果不成功返回 0. 函数原型 size_t fread ( void ...