1520. Maximum Number of Non-Overlapping Substrings
</> Solution
/**
* Problem: LeetCode 1520 - Maximum Number of Non-Overlapping Substrings
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Problem: LeetCode 1520 - Maximum Number of Non-Overlapping Substrings
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public List<String> maxNumOfSubstrings(String s) {
int n = s.length();
int[] first = new int[26];
int[] last = new int[26];
Arrays.fill(first, Integer.MAX_VALUE);
for (int i = 0; i < n; i++) {
int c = s.charAt(i) - 'a';
first[c] = Math.min(first[c], i);
last[c] = Math.max(last[c], i);
}
List<String> result = new ArrayList<>();
int prevEnd = -1;
for (int i = 0; i < n; i++) {
int c = s.charAt(i) - 'a';
if (first[c] != i) continue;
int l = i;
int r = last[c];
boolean valid = true;
for (int j = l; j <= r; j++) {
int ch = s.charAt(j) - 'a';
if (first[ch] < l) {
valid = false;
break;
}
r = Math.max(r, last[ch]); // expand window
}
if (!valid) continue;
if (l > prevEnd) {
result.add(s.substring(l, r + 1));
prevEnd = r;
} else if (r < prevEnd) {
result.set(result.size() - 1, s.substring(l, r + 1));
prevEnd = r;
}
}
return result;
}
}
"""
Problem: LeetCode 1520 - Maximum Number of Non-Overlapping Substrings
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def maxNumOfSubstrings(self, s: str) -> List[str]:
n = len(s)
first = [float('inf')] * 26
last = [-1] * 26
for i, ch in enumerate(s):
c = ord(ch) - ord('a')
first[c] = min(first[c], i)
last[c] = max(last[c], i)
result = []
prev_end = -1
for i in range(n):
c = ord(s[i]) - ord('a')
if first[c] != i:
continue
l = i
r = last[c]
valid = True
j = l
while j <= r:
ch = ord(s[j]) - ord('a')
if first[ch] < l:
valid = False
break
r = max(r, last[ch])
j += 1
if not valid:
continue
if l > prev_end:
result.append(s[l:r + 1])
prev_end = r
elif r < prev_end:
result[-1] = s[l:r + 1]
prev_end = r
return result
/**
* Problem: LeetCode 1520 - Maximum Number of Non-Overlapping Substrings
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <string>
#include <algorithm>
#include <climits>
/**
* Problem: LeetCode 1520 - Maximum Number of Non-Overlapping Substrings
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
std::vector<std::string> maxNumOfSubstrings(std::string s) {
int n = s.length();
std::vector<int> first(26, INT_MAX);
std::vector<int> last(26, -1);
for (int i = 0; i < n; i++) {
int c = s[i] - 'a';
first[c] = std::min(first[c], i);
last[c] = std::max(last[c], i);
}
std::vector<std::string> result;
int prevEnd = -1;
for (int i = 0; i < n; i++) {
int c = s[i] - 'a';
if (first[c] != i) continue;
int l = i;
int r = last[c];
bool valid = true;
for (int j = l; j <= r; j++) {
int ch = s[j] - 'a';
if (first[ch] < l) {
valid = false;
break;
}
r = std::max(r, last[ch]);
}
if (!valid) continue;
if (l > prevEnd) {
result.push_back(s.substr(l, r - l + 1));
prevEnd = r;
} else if (r < prevEnd) {
result.back() = s.substr(l, r - l + 1);
prevEnd = r;
}
}
return result;
}
};
/**
* Problem: LeetCode 1520 - Maximum Number of Non-Overlapping Substrings
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
var maxNumOfSubstrings = function(s) {
const n = s.length;
const first = new Array(26).fill(Infinity);
const last = new Array(26).fill(-1);
for (let i = 0; i < n; i++) {
const c = s.charCodeAt(i) - 97;
first[c] = Math.min(first[c], i);
last[c] = Math.max(last[c], i);
}
const result = [];
let prevEnd = -1;
for (let i = 0; i < n; i++) {
const c = s.charCodeAt(i) - 97;
if (first[c] !== i) continue;
let l = i;
let r = last[c];
let valid = true;
for (let j = l; j <= r; j++) {
const ch = s.charCodeAt(j) - 97;
if (first[ch] < l) {
valid = false;
break;
}
r = Math.max(r, last[ch]);
}
if (!valid) continue;
if (l > prevEnd) {
result.push(s.slice(l, r + 1));
prevEnd = r;
} else if (r < prevEnd) {
result[result.length - 1] = s.slice(l, r + 1);
prevEnd = r;
}
}
return result;
};