leetcode 466. Count The Repetitions
来源:互联网 发布:hr抢购软件 编辑:程序博客网 时间:2024/06/14 10:41
leetcode 466. Count The Repetitions
这一题主要考察复杂问题的简化求解思路,从简单的例子着手。
public class Solution { public int getMaxRepetitions(String s1, int n1, String s2, int n2) { if (!ableToObtain(s1, s2)) return 0; // check if [s1. ∞] obtains s2 int cnt=0, k=-1; String s=s1; StringBuilder remainBuilder; // record `remain string` ArrayList<String> stringList=new ArrayList<>(); // record all the `remain string` ArrayList<Integer> countList=new ArrayList<>(); // record matching count from start to the current remain string stringList.add(""); // record empty string countList.add(0); for (int i=0;i<=n1;i++) { remainBuilder=new StringBuilder(); cnt+=getRemain(s, s2, remainBuilder); // get the next remain string, returns the count of matching String remain=remainBuilder.toString(); if ((k=stringList.indexOf(remain))!=-1) break; // if there is a loop, break stringList.add(remain); // record the remain string into arraylist countList.add(cnt); s=remain+s1; // append s1 to make a new string } // here, k is the beginning of the loop if (k==-1) return cnt/n2; // if there is no loop int countOfLoop=cnt-countList.get(k), loopLength=stringList.size()-k; // get matching count in the loop, and loop length cnt=countList.get(k); n1-=k; cnt+=countOfLoop*(n1/loopLength); n1%=loopLength; cnt+=countList.get(k+n1)-countList.get(k); return cnt/n2; } // check if [s1. ∞] obtains s2 private boolean ableToObtain(String s1, String s2) { boolean[] cnt=new boolean[26]; for (char c: s1.toCharArray()) cnt[c-'a']=true; for (char c: s2.toCharArray()) { if (!cnt[c-'a']) return false; } return true; } // get remain string after s1 obtains s2, return the matching count private int getRemain(String s1, String s2, StringBuilder remain) { int cnt=0, lastMatch=-1, p2=0; for (int p1=0;p1<s1.length();p1++) { if (s1.charAt(p1)==s2.charAt(p2)) { if (++p2==s2.length()) { p2=0; cnt++; lastMatch=p1; } } } remain.append(s1.substring(lastMatch+1)); return cnt; }}
阅读全文
0 0
- leetcode 466. Count The Repetitions
- leetcode 466. Count The Repetitions
- [Leetcode]Count The Repetitions
- [LeetCode] Count The Repetitions
- [Leetcode] 466. Count The Repetitions 解题报告
- 466. Count The Repetitions
- 466. Count The Repetitions
- leetcode:count the number of nodes
- Getting the CPU count
- hdu3336 Count the string
- 【線段樹】Count the Colors
- UVa10007 - Count the Trees
- hdu3336 Count the string
- Count the colors
- 10699 - Count the factors
- ZOJ1610-Count the Colors
- zoj1610-Count the Colors
- HDU3336:Count the string
- JAVA NIO(二):小端字节顺序
- Java.util.Arrays.deepToString(Object[])
- CentOS配置服务器常用指令
- oracle---合并geometry
- Mongodb从0到1系列五: 主从复制
- leetcode 466. Count The Repetitions
- 用php代码获取机器的ip地址
- openssh-server
- Mybatis中使用oracle的模糊查询的SQL语句写法
- Compile and build specific Hadoop source code branch using Azure VM
- POJ 2823 Sliding Window
- Jvm内存溢出的几种情况
- springMVC常用注解
- hadoop源码编译、配置安装、测试