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:

 1 class Solution {
 2     public int maxRepOpt1(String text) {
 3         if(text == null || text.length() == 0){
 4             return 0;
 5         }
 6
 7         int len = text.length();
 8         int [] map = new int[26];
 9         List<Pair> groupsList = new ArrayList<>();
10         int i = 0;
11
12         while(i < len){
13             char c = text.charAt(i);
14             int f = 0;
15             while(i < len && text.charAt(i) == c){
16                 f++;
17                 i++;
18             }
19
20             groupsList.add(new Pair(c, f));
21             map[c-‘a‘] += f;
22         }
23
24         int max = 0;
25         for(int j = 0; j<groupsList.size(); j++){
26             Pair cur = groupsList.get(j);
27
28             // Single group
29             max = Math.max(max, Math.min(cur.f+1, map[cur.c - ‘a‘]));
30
31             // Two groups
32             if(j < groupsList.size() - 2){
33                 if(groupsList.get(j+1).f == 1 && cur.c == groupsList.get(j+2).c){
34                     max = Math.max(max, Math.min(cur.f + groupsList.get(j+2).f + 1, map[cur.c - ‘a‘]));
35                 }
36             }
37         }
38
39         return max;
40     }
41 }
42
43 class Pair{
44     char c;
45     int f;
46     public Pair(char c, int f){
47         this.c = c;
48         this.f = f;
49     }
50 }

原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/12004508.html

时间: 2024-08-29 02:23:50

LeetCode 1156. Swap For Longest Repeated Character Substring的相关文章

ZOJ 3199 Longest Repeated Substring

Longest Repeated Substring Time Limit: 5000ms Memory Limit: 32768KB This problem will be judged on ZJU. Original ID: 319964-bit integer IO format: %lld      Java class name: Main Write a program that takes a string and returns length of the longest r

LeetCode 第 3 题(Longest Substring Without Repeating Characters)

LeetCode 第 3 题(Longest Substring Without Repeating Characters) Given a string, find the length of the longest substring without repeating characters. Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "b

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 这是一个简单版本of LC 424 Longest Repeating Character Replacement 又是Window, 又是Two Pointers Window还是采用每次都try to update left使得window valid, 每次都检查最大window 1 package

【leetcode刷题笔记】Minimum Window Substring

Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n). For example,S = "ADOBECODEBANC"T = "ABC" Minimum window is "BANC". Note:If there is no such window i

LeetCode之“动态规划”:Longest Valid Parentheses

题目链接 题目要求: Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another e

leetcode第31题--Longest Valid Parentheses

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example is &

【LeetCode】Swap Nodes in Pairs

题目 Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1->2->3->4, you should return the list as 2->1->4->3. Your algorithm should use only constant space. You may not modify the values in the list,

[LeetCode] 024. Swap Nodes in Pairs (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 024. Swap Nodes in Pairs (Medium) 链接: 题目:https://oj.leetcode.com/problems/swap-nodes-in-pairs/ 代码(github):https://github.com/illuz/leetcode 题意: 把一个链表中的每一对节点对换

LeetCode【3】.Longest Substring Without Repeating Characters--算法图解及java实现

第三道题Longest Substring Without Repeating Characters如下:     Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", whic