LeetCode 1156. Swap For Longest Repeated Character Substring
原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/
题目:
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
Example 1:
Input: text = "ababa"
Output: 3
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
Example 2:
Input: text = "aaabaaa"
Output: 6
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
Example 3:
Input: text = "aaabbaaa"
Output: 4
Example 4:
Input: text = "aaaaa"
Output: 5
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
Example 5:
Input: text = "abcdef"
Output: 1
Constraints:
1 <= text.length <= 20000textconsist of lowercase English characters only.
题解:
There could be 2 cases to achieve the fulfilled longest substring.
case 1: One block containing longest. And then replace one boundary char to be the same, and get len+1.
case 2: Two blocks containing same chars separated by 1 single different char. In this case, the single different char could be replaced.
Both cases, it needs to make sure that there are extra same chars.
Time Complexity: O(n). n = text.length.
Space: O(n).
AC Java:
class Solution {
public int maxRepOpt1(String text) {
if(text == null || text.length() == 0){
return 0;
}
int len = text.length();
int [] map = new int[26];
List<Pair> groupsList = new ArrayList<>();
int i = 0;
while(i < len){
char c = text.charAt(i);
int f = 0;
while(i < len && text.charAt(i) == c){
f++;
i++;
}
groupsList.add(new Pair(c, f));
map[c-'a'] += f;
}
int max = 0;
for(int j = 0; j<groupsList.size(); j++){
Pair cur = groupsList.get(j);
// Single group
max = Math.max(max, Math.min(cur.f+1, map[cur.c - 'a']));
// Two groups
if(j < groupsList.size() - 2){
if(groupsList.get(j+1).f == 1 && cur.c == groupsList.get(j+2).c){
max = Math.max(max, Math.min(cur.f + groupsList.get(j+2).f + 1, map[cur.c - 'a']));
}
}
}
return max;
}
}
class Pair{
char c;
int f;
public Pair(char c, int f){
this.c = c;
this.f = f;
}
}
LeetCode 1156. Swap For Longest Repeated Character Substring的更多相关文章
- 【leetcode】1156. Swap For Longest Repeated Character Substring
题目如下: Given a string text, we are allowed to swap two of the characters in the string. Find the leng ...
- ZOJ 3199 Longest Repeated Substring
Longest Repeated Substring Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on Z ...
- LeetCode 第 3 题(Longest Substring Without Repeating Characters)
LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...
- G面经Prepare: Longest All One Substring
give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping 这是一 ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- ACM Longest Repeated Sequence
Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...
- Longest Repeated Sequence【微软编程一小时-题目2】
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of integers, A = a1, a2, ... an. A c ...
- leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
随机推荐
- 关于C语言指针的讨论
C语言指针的讨论 1.指整的概念辨析 2.指针与一维数组 3.指针与二维数组 4.指针与动态数组 5.指针数组 6. 指整与函数,形参,返回值 先熟悉一下概念,使劲把他们记下了 变量定义 类型表示 含 ...
- 【搬运工】RHEL6.5 移植使用CentOS 的YUM 步骤
转载地址:http://www.cnblogs.com/rchen98/p/6056469.html 问题:使用 Red Hat Enterprise Linux Server(RHEL) yum安装 ...
- Python格式化输出——format用法示例
format OR % 提到Python中的格式化输出方法,一般来说有以下两种方式: print('hello %s' % 'world') # hello world print('hello {} ...
- 转Tasklist(windows)
Windows 进程 Tasklist查看 与 Taskkill结束 转自https://blog.csdn.net/wangmx1993328/article/details/80923829 ...
- Codility MinMaxDivision
最近发现了一个刷题网站:https://app.codility.com/programmers/lessons 这个网站做题目时候的界面让我惊艳到了 首先这是题目界面: 然后点击start, 出来的 ...
- Elasticsearch常见用法-分布式集群
集群内部工作方式 Elasticsearch用于构建高可用和可扩展的系统.扩展的方式可以是购买更好的服务器(纵向扩展(vertical scale or scaling up))或者购买更多的服务器( ...
- 程序员不装x能行?先给登录来一个图形验证码!(canvas实现)
细心的同学可以发现,现在很多网站当登录多次之后就会出现一个图形验证码,或是当提交表单.或点击获取手机验证码等等场景都会有图形验证码的出现. 那么图形验证码是为了解决什么问题而出现的呢? 什么是图形验证 ...
- Java的表达式和运算符
一.算术运算符 运算符 + - * / % 说明 加 减 乘 除 取模(余数) 例子 1+2 5-3 20*5 6/4 30%9 结果 3 2 100 1 3 int x = 10; int y = ...
- using 语句(C# 参考)(转载)
using 语句 提供可确保正确使用 IDisposable对象的方便语法. 示例 下面的示例演示如何使用 using 语句. using (var font1 = new Font("Ar ...
- (原创)如何搭建PLC+上位机监控系统达到成本的最小化?
以西门子PLC举例; 西门子PLC有几个型号:S7-200SMART,S7-1200,S7-300,S7-400,S7-1500,价格从低到高. 1个项目中要求的IO数量:600点的DI+DO,若干个 ...