longest-repeating-character-replacement(难)
用sliding window的方法,之前还有个k不同元素好像也是类似的思路。有时间可以去复习下。
https://leetcode.com/problems/longest-repeating-character-replacement/
// sliding window
public class Solution {
    public int characterReplacement(String s, int k) {
        int[] count = new int[26];
        int maxch = -1; // the char with max count in current substr
        int max = 0; // the max count for single char in current substr
        int len = 0; // current substr length
        int ret = 0; // final result
        for (int i=0; i<s.length(); i++) {
            int tmp = s.charAt(i) - 'A';
            count[tmp]++;
            len++;
            if (maxch == tmp) {
                max++;
            }
            else {
                if (count[tmp] > max) {
                    max = count[tmp];
                    maxch = tmp;
                }
            }
            if (len - max <= k) {
                if (len > ret) {
                    ret = len;
                }
            }
            while (len - max > k) {
                int newTmp = s.charAt(i-len+1) - 'A';
                count[newTmp]--;
                len--;
                if (maxch == newTmp) {
                    max--;
                    for (int j=0; j<26; j++) {
                        if (count[j] > max) {
                            max = count[j];
                            maxch = j;
                        }
                    }
                }
            }
        }
        return ret;
    }
}
longest-repeating-character-replacement(难)的更多相关文章
- Leetcode: Longest Repeating Character Replacement && G 面经
		
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
 - [Swift]LeetCode424. 替换后的最长重复字符 | Longest Repeating Character Replacement
		
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
 - G 面经 && Leetcode: Longest Repeating Character Replacement
		
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
 - LeetCode——Longest Repeating Character Replacement
		
1. Question Given a string that consists of only uppercase English letters, you can replace any lett ...
 - leetcode424  Longest Repeating Character Replacement
		
""" Given a string s that consists of only uppercase English letters, you can perform ...
 - [LeetCode] Longest Repeating Character Replacement 最长重复字符置换
		
Given a string that consists of only uppercase English letters, you can replace any letter in the st ...
 - LeetCode 424. Longest Repeating Character Replacement
		
原题链接在这里:https://leetcode.com/problems/longest-repeating-character-replacement/description/ 题目: Given ...
 - 【leetcode】424. Longest Repeating Character Replacement
		
题目如下: Given a string that consists of only uppercase English letters, you can replace any letter in ...
 - 【LeetCode】424. 替换后的最长重复字符 Longest Repeating Character Replacement(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,字符串,双指针,刷题群 目录 题目描述 题目大意 解题方法 双指针 代码 欢迎 ...
 - 424. Longest Repeating Character Replacement
		
以最左边为开始,往右遍历,不一样的个数大于K的时候停止,回到第一个不一样的地方,以它为开始,继续.. 用QUEUE记录每次不一样的INDEX,以便下一个遍历开始, 从左往右,从右往左各来一次..加上各 ...
 
随机推荐
- HDU-4255
			
A Famous Grid Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
 - hdu 1853(拆点判环+费用流)
			
Cyclic Tour Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/65535 K (Java/Others)Total ...
 - Balanced Binary Tree——经典题
			
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
 - 解决错误:此用户名包含无效字符,请输入有效的用户名。wordpress不能注册中文用户名的问题
			
wordpress在默认情况下不支持中文用户名,就是在后台添加用户的时候,如果用户名包含中文,则显示”错误:此用户名包含无效字符,请输入有效的用户名.”如何解决这个问题呢? 不用插件的话就需要修改一个 ...
 - AC日记——妖梦斩木棒 洛谷 P3797
			
妖梦斩木棒 思路: 略坑爹: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 200005 #define m ...
 - ASP.NET MVC 视图学习,纯干货
			
最近用MVC专门为自己做了一个网站,用来记录文章心情和日记.加上和同事的一些交流感觉颇深.所以想把13年买的MVC 4高级编程重新看一遍,记录一些东西,以后应该用的到.视图总是被控制器渲染,因为控制器 ...
 - 用Lucene.net对数据库建立索引及搜索<转>
			
用Lucene.net对数据库建立索引及搜索 最近我一直在研究 Lucene.net ,发现Lucene.net对数据库方面建索引的文章在网上很少见,其实它是可以对数据库进行索引的,我闲着没事,写了个 ...
 - SSH整合错误三连
			
访问Action错误 ognl.MethodFailedException: Method "add" failed for object com.test3.action.Use ...
 - oracle中 char,varchar,varchar2的区别
			
区别: 1. CHAR的长度是固定的,而VARCHAR2的长度是可以变化的, 比如,存储字符串“abc",对于CHAR (20),表示你存储的字符将占20个字节(包括17个空字符) ...
 - A/B Problem(大数)
			
描述 做了A+B Problem,A/B Problem不是什么问题了吧! 输入 每组测试样例一行,首先一个号码A,中间一个或多个空格,然后一个符号( / 或者 % ),然后又是空格,后面又是一个号码 ...