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 <= 20000
text
consist 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 ...
随机推荐
- mysql中,手动提交事务
1: 在mysql中,手动提交事务的案例:CREATE PROCEDURE tfer_funds (from_account int, to_account int, tfer_amoun ...
- servlet是一组规范--Servlet是JavaEE规范的一种
Java Servlet API是Servlet容器和Servlet之间的接U,它定义了Servlet的各种方法, 还定义了Servlet容器传送给Servlet的对象类,其中最重要的是请求对象Ser ...
- 示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本
原文:示例:自定义WPF底层控件UI库 HeBianGu.General.WpfControlLib V2.0版本 一.目的:封装了一些控件到自定义的控件库中,方便快速开发 二.实现功能: 基本实现常 ...
- .Net Core WebAPI开启静态页,设置主页
1.使用场景 默认创建的.Net Core WebAPI应用在运行时是没有页面显示的,效果如下: 那么,如果想要给API设置一个主页,应该怎么做呢?这就需要用到本文提供的方法. 2.设置方法 (1)首 ...
- Apache Tomcat 9.0 Tomcat9 服务无法启动。发生服务特定错误: 4.
在Tomcat的安装目录下,bin文件夹里面 找到tomcat9w.exe 双击进去,将第四页java里面第一个复选框Use default 选中 保存即可启动tomcat9服务
- jsp,servlet文件上传问题完善
1. 上传文件时文件名中文乱码 upload.setHeaderEncoding("utf-8"); 有个疑惑: 不管设置不设置都不乱码,但是刘帅龙老师讲的时候出现了乱码 . 2. ...
- python 中 try,except,finally 的执行顺序
写代码的时候发现了好玩的事情,常常作为终止的 return 语句并不总是能够立刻跳出函数 def A(): try: for i in range(10): if i == 5: return pri ...
- ES6 笔记汇总
ES6 笔记汇总 二.ES6基础-let和const命令 三.变量的解构赋值 四.字符串的拓展 五.正则表达式的拓展 ...将会持续更新,敬请期待
- Qt使用QPainter绘制矢量图并保存为svg文件
位图和矢量图: Bitmap: Usually a larger file size Cannot be enlarged into a higher resolution as the image ...
- 面试题:android的安全机制有哪些
1 uid . gid . gids Android 的权限分离的基础是建立在 Linux 已有的 uid . gid . gids 基础上的 . UID: Android 在 安装一个应用程序,就会 ...