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 ...
随机推荐
- idea 跳转提示多个实现类
- SQL系列(六)—— 过滤(where)
在日常的应用中的,大多数业务场景都只是需要特定的数据,所以能够过滤筛选数据显得尤为至关重要.从需求角度分析,需要特定的数据,即需要一定条件的数据,即从全量数据中根据特定条件过滤出需要的数据. 如果需要 ...
- WCF学习笔记(一)---我的第一个WCF程序
一.创建WCF程序 1.创建一个控制台程序(WCFBlog) 2.添加wcf项目 3.将默认的IService1和Service1改成自己的名字 4.在ICalculateServic ...
- 解决Spring Boot 从1.x升级到 2.x 后 单点登陆(SSO)问题
解决Spring Boot 从1.x升级到 2.x 后 单点登陆(SSO)问题 在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring S ...
- HTML列表简单介绍
1.无序列表 使用标签:<ul>,<li> 属性:disc,circle,square 2.有序列表 使用标签:<ol>,<li> 属性:A,a,I,i ...
- DataPipeline丨「自定义」数据源,解决复杂请求逻辑外部数据获取难题
A公司专注为各种规模和复杂程度的金融投资机构提供一体化投资管理系统,系统主要由投资组合管理.交易执行管理.实时监控管理.风险管理等功能模块构成.随着企业管理产品数量的不断增多,大量数据分散在各券商系统 ...
- 如何使用Python的Django框架创建自己的网站
如何使用Python的Django框架创建自己的网站 Django建站主要分四步:1.创建Django项目,2.将网页模板移植到Django项目中,3.数据交互,4.数据库 1创建Django项目 本 ...
- EXCHANGE上冒充任意用户--Exchange Server权限提升漏洞(CVE-2018-8581)分析
0x00 前言 这是我们2018年Top 5趣案系列中的第三个案例.这些漏洞都有一些因素使它们从今年发布的大约1,400个报告中脱颖而出.今天我们将分析一个Exchange漏洞,它允许任何经过身份验证 ...
- Objective-C之深浅拷贝
深拷贝(指针和指向都改变) , 浅拷贝(指针改变,指向不变) NSString *s1 = @"string"; NSLog(@"s1 : %p, %p, %@" ...
- SAP技术 - How to create a CDS redirect view for a given database table
Scenario Suppose we have a database table A, and then we create a CDS redirect view B for it, then e ...