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 ...
随机推荐
- 08 Servlet
Servlet技术 * Servlet开发动态的Web资源的技术. * Servlet技术 * 在javax. ...
- 2015 ACM Syrian Collegiate Programming Contest
A. My Friend of Misery 计算出答案的上下界即可. 时间复杂度$O(n)$. #include<bits/stdc++.h> using namespace std; ...
- Wordpress 所有hoor列表
d 在插件加载的时候执行 wp_footer 加载页面底部时执行 admin_menu 加载管理员菜单时执行 wp_head 在body标签的开始添加html内容 after_setup_theme ...
- OSG的节点访问
OSG的节点访问 转自:http://www.cnblogs.com/kanego/archive/2011/09/27/2193484.html SG中节点的访问使用的是一种访问器模式. 一个典型的 ...
- 理解margin
margin可以改变容器的尺寸 //元素尺寸分为可视尺寸,占据尺寸 margin与可视尺寸 1.适用于没有设定width/height的普通block水平元素 2.只适用于水平方向的尺寸 应用:一侧定 ...
- MySQL支持的数据类型(2)( 日期)
日期和时间类型 字节 最小值 最大值 date 4 1000-01-01 9999-12-31 datetime 8 1000-01-01 00:00:00 9999-12-31 23:59:59 t ...
- 从DataGridView导出Excel
从DataGridView导出Excel的两种情况,不多说,直接记录代码(新建类,直接引用传入参数). using System; using System.Collections.Generic; ...
- thinkphp1
命名空间 含义:从广义上来说,命名空间是一种封装事物的方法. 用途:用来解决命名冲突 namespace xxx\xxx; 使用: use xxx\xx\yy; new\xx\xx\yy; // 单一 ...
- defer 和 async 本地对象和宿主对象
<script async></script> 没有defer和async的情况下是"同步执行"的,浏览器解析到这里的时候,会先加载资源完成后立即执行,并阻 ...
- 中國區的代理協議的韓國遊戲廠商PatiGames
“與阿裏巴巴簽署旗下游戲「突突三國」在中國區的代理協議的韓國遊戲廠商PatiGames決定與阿裏巴巴終止合作.”相信這條前不久報導的新聞,很多人並不陌生,但這背後其實並不像表面那樣簡單.早在今年4月P ...