3302. Find the Lexicographically Smallest Valid Sequence
</> Solution
/**
* Problem: LeetCode 3302 - Find the Lexicographically Smallest Valid Sequence
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
import java.util.Arrays;
/**
* Problem: LeetCode 3302 - Find the Lexicographically Smallest Valid Sequence
* Difficulty: Medium
* Language: Java
* Author: CodeByTushu
*/
class Solution {
public int[] validSequence(String word1, String word2) {
int[] ans = new int[word2.length()];
// last[j] := the index i of the last occurrence in word1, where
// word1[i] == word2[j]
int[] last = new int[word2.length()];
Arrays.fill(last, -1);
int i = word1.length() - 1;
int j = word2.length() - 1;
while (i >= 0 && j >= 0) {
if (word1.charAt(i) == word2.charAt(j))
last[j--] = i;
--i;
}
boolean canSkip = true;
j = 0;
for (i = 0; i < word1.length(); ++i) {
if (j == word2.length())
break;
if (word1.charAt(i) == word2.charAt(j)) {
ans[j++] = i;
} else if (canSkip && (j == word2.length() - 1 || i < last[j + 1])) {
canSkip = false;
ans[j++] = i;
}
}
return j == word2.length() ? ans : new int[0];
}
}
"""
Problem: LeetCode 3302 - Find the Lexicographically Smallest Valid Sequence
Difficulty: Medium
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def validSequence(self, word1: str, word2: str) -> List[int]:
n, m = len(word1), len(word2)
ans = [0] * m
last = [-1] * m
# last[j] stores the latest index i in word1 matching word2[j]
# such that the suffix word2[j...m-1] can be matched in word1[i...]
i, j = n - 1, m - 1
while i >= 0 and j >= 0:
if word1[i] == word2[j]:
last[j] = i
j -= 1
i -= 1
can_skip = True
j = 0
for i in range(n):
if j == m:
break
if word1[i] == word2[j]:
ans[j] = i
j += 1
elif can_skip and (j == m - 1 or i < last[j + 1]):
can_skip = False
ans[j] = i
j += 1
return ans if j == m else []
/**
* Problem: LeetCode 3302 - Find the Lexicographically Smallest Valid Sequence
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
#include <iostream>
#include <string>
#include <vector>
using namespace std;
/**
* Problem: LeetCode 3302 - Find the Lexicographically Smallest Valid Sequence
* Difficulty: Medium
* Language: C++
* Author: CodeByTushu
*/
class Solution {
public:
vector<int> validSequence(string word1, string word2) {
int n = word1.length();
int m = word2.length();
vector<int> ans(m);
vector<int> last(m, -1);
// last[j] stores the latest index i in word1 matching word2[j]
// such that the suffix word2[j...m-1] can be matched in word1[i...]
int i = n - 1;
int j = m - 1;
while (i >= 0 && j >= 0) {
if (word1[i] == word2[j]) {
last[j--] = i;
}
--i;
}
bool canSkip = true;
j = 0;
for (i = 0; i < n; ++i) {
if (j == m) break;
if (word1[i] == word2[j]) {
ans[j++] = i;
} else if (canSkip && (j == m - 1 || i < last[j + 1])) {
canSkip = false;
ans[j++] = i;
}
}
return j == m ? ans : vector<int>();
}
};
/**
* Problem: LeetCode 3302 - Find the Lexicographically Smallest Valid Sequence
* Difficulty: Medium
* Language: JavaScript
* Author: CodeByTushu
*/
var validSequence = function(word1, word2) {
const n = word1.length;
const m = word2.length;
const ans = new Array(m);
const last = new Array(m).fill(-1);
// last[j] stores the latest index i in word1 matching word2[j]
// such that the suffix word2[j...m-1] can be matched in word1[i...]
let i = n - 1;
let j = m - 1;
while (i >= 0 && j >= 0) {
if (word1[i] === word2[j]) {
last[j] = i;
j--;
}
i--;
}
let canSkip = true;
j = 0;
for (i = 0; i < n; i++) {
if (j === m) break;
if (word1[i] === word2[j]) {
ans[j++] = i;
} else if (canSkip && (j === m - 1 || i < last[j + 1])) {
canSkip = false;
ans[j++] = i;
}
}
return j === m ? ans : [];
};