原题链接在这里: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的更多相关文章

  1. 【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 ...

  2. ZOJ 3199 Longest Repeated Substring

    Longest Repeated Substring Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on Z ...

  3. LeetCode 第 3 题(Longest Substring Without Repeating Characters)

    LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of th ...

  4. 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 这是一 ...

  5. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  6. ACM Longest Repeated Sequence

    Description You are given a sequence of integers, A = a1, a2, ... an. A consecutive subsequence of A ...

  7. Longest Repeated Sequence【微软编程一小时-题目2】

    时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 You are given a sequence of integers, A = a1, a2, ... an. A c ...

  8. leetcode第31题--Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

随机推荐

  1. 【模板】LCT

    核心思想: 动态维护一个森林.支持删边,加边,查询链信息等很多操作. 由若干棵$Splay$组成,每棵$Splay$维护一条链,以深度作为关键字. 也就是说$Splay$的中序遍历相当于从上到下遍历这 ...

  2. Implementing Azure AD Single Sign-Out in ASP.NET Core(转载)

    Let's start with a scenario. Bob the user has logged in to your ASP.NET Core application through Azu ...

  3. js获取简单表单对象(1)

    <form id="form">     <input type="text" name="username" value ...

  4. nc 从服务器上传下载文件

    1.安装 nc # yum install nc -y 2.下载文件 // 在 45.77.17.128 这台主机监听 9988 端口(注意符号是 "<" ) # nc -l ...

  5. python基础2--if,while,for,逻辑运算

    1.1 条件控制语句 1.if - elif - else 2.常用操作运算符 < > >= <=  == != 3.if elif 后面一定要有条件 else后面没有条件 1 ...

  6. 使用 HttpWebRequest 类做 POST 请求没有应反

    这几天给系统做第三方集成, 需要调用另一个软件的一个接口, 通过 HTTP 的方式调用,调用代码也挺简单的: string serviceUrl = string.Format("{0}/{ ...

  7. Java小知识点总结01

    1. 整数相乘或者相加,如果超过最大整数值,会变成负数 2. 字符串比较可以使用:s1.compareTo(s2) ,如果s1大于s2返回值大于1,等于返回值等于0,小于返回值小于1 3. char值 ...

  8. ASP.NET Core中返回 json 数据首字母大小写问题

    ASP.NET Core中返回 json 数据首字母大小写问题 在asp.net core中使用ajax请求动态绑定数据时遇到该问题 后台返回数据字段首字母为定义的大写,返回的数据没有问题 但是在前台 ...

  9. 2019最新Android常用开源库总结(附带github链接)

    前言 收集了一些比较常见的开源库,特此记录(已收录350+).另外,本文将持续更新,大家有关于Android 优秀的开源库,也可以在下面留言. 一 .基本控件 1.TextView HTextView ...

  10. React: 有状态组件生成真实DOM结点

    上次我们分析了无状态组件生成 DOM 的过程,无状态组件其实就是纯函数,它不维护内部的状态,只是根据外部输入,输出一份视图数据.而今天我们介绍的有状态组件,它有内部的状态,因此在组件的内部,可以自行对 ...