Leetcode: Count The Repetitions
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的更多相关文章
- [LeetCode] Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- CH5702 Count The Repetitions
题意 5702 Count The Repetitions 0x50「动态规划」例题 描述 定义 conn(s,n) 为 n 个字符串 s 首尾相接形成的字符串,例如: conn("abc& ...
- 【leetcode 字符串】466. Count The Repetitions
https://leetcode.com/problems/count-the-repetitions/description/ 找循环节 https://www.cnblogs.com/grandy ...
- 第七周 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个 ...
- [LeetCode] 466. Count The Repetitions 计数重复个数
Define S = [s,n] as the string S which consists of n connected strings s. For example, ["abc&qu ...
- [LeetCode] Count Numbers with Unique Digits 计算各位不相同的数字个数
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n. Examp ...
- [LeetCode] Count of Range Sum 区间和计数
Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.Ra ...
- [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 ...
- [LeetCode] Count Univalue Subtrees 计数相同值子树的个数
Given a binary tree, count the number of uni-value subtrees. A Uni-value subtree means all nodes of ...
随机推荐
- Codeforces Round #353 (Div. 2)
数学 A - Infinite Sequence 等差数列,公差是0的时候特判 #include <bits/stdc++.h> typedef long long ll; const i ...
- SOAPUI使用教程-MockOperations和响应
如前所述,一个MockService有多个MockOperations其中每个可以包含任意数量的MockResponse消息; 也就是说,一个MockService响应实际上包括若干预设响应之间发生变 ...
- Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)
概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...
- BZOJ 3157 &数学乱搞...
挖一发坑...一碰到数学题头晕眼花...看着证明感觉我幼儿园还没毕业... http://www.cnblogs.com/zhuohan123/p/3726933.html 等我会完完整整地写一发la ...
- 洛谷 P2725 邮票 Stamps Label:DP
题目背景 给一组 N 枚邮票的面值集合(如,{1 分,3 分})和一个上限 K —— 表示信封上能够贴 K 张邮票.计算从 1 到 M 的最大连续可贴出的邮资. 题目描述 例如,假设有 1 分和 3 ...
- 解决使用osgModeling的Loft生成管子时的bug
最近在使用osgModeling的Loft生成管子的时候, 发现这个类还是有点bug的. 具体的表现就是在某些情况下, 生成管子的某些节点会是扁的, 而且有时管子会莫名的变粗. 在网上各种求助无果 ...
- thinkphp pathinfo nginx 无法加载模块:Index
thinkphp 报了 无法加载模块:Index 错误位置 FILE: /var/multrix/wxactivity_archive/ThinkPHP/Library/Think/Dispatche ...
- 启动 apache2.4 出现 invalid command order 问题 【由于 PHP 访问权限 403 问题引起】
由于网站出现 403 禁止访问,所以修改了 httpd.conf 文件的权限如下 <Directory /> Options FollowSymLinks AllowOverride N ...
- Multiprocessor Operating System Design Considerations SYMMETRIC MULTIPROCESSORS
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION An SMP operating syst ...
- SQL CREATE TABLE 语句\SQL 约束 (Constraints)\SQL NOT NULL 约束\SQL UNIQUE 约束
CREATE TABLE 语句 CREATE TABLE 语句用于创建数据库中的表. SQL CREATE TABLE 语法 CREATE TABLE 表名称 ( 列名称1 数据类型, 列名称2 数据 ...