Welcome to Subscribe On Youtube

3302. Find the Lexicographically Smallest Valid Sequence

Description

You are given two strings word1 and word2.

A string x is called almost equal to y if you can change at most one character in x to make it identical to y.

A sequence of indices seq is called valid if:

  • The indices are sorted in ascending order.
  • Concatenating the characters at these indices in word1 in the same order results in a string that is almost equal to word2.

Return an array of size word2.length representing the lexicographically smallest valid sequence of indices. If no such sequence of indices exists, return an empty array.

Note that the answer must represent the lexicographically smallest array, not the corresponding string formed by those indices.

 

Example 1:

Input: word1 = "vbcca", word2 = "abc"

Output: [0,1,2]

Explanation:

The lexicographically smallest valid sequence of indices is [0, 1, 2]:

  • Change word1[0] to 'a'.
  • word1[1] is already 'b'.
  • word1[2] is already 'c'.

Example 2:

Input: word1 = "bacdc", word2 = "abc"

Output: [1,2,4]

Explanation:

The lexicographically smallest valid sequence of indices is [1, 2, 4]:

  • word1[1] is already 'a'.
  • Change word1[2] to 'b'.
  • word1[4] is already 'c'.

Example 3:

Input: word1 = "aaaaaa", word2 = "aaabc"

Output: []

Explanation:

There is no valid sequence of indices.

Example 4:

Input: word1 = "abc", word2 = "ab"

Output: [0,1]

 

Constraints:

  • 1 <= word2.length < word1.length <= 3 * 105
  • word1 and word2 consist only of lowercase English letters.

Solutions

Solution 1

  • class Solution {
        public int[] validSequence(String word1, String word2) {
            int m = word1.length(), n = word2.length();
    
            int[] suf = new int[m + 1];
            suf[m] = n;
    
            int j = n - 1;
            for (int i = m - 1; i >= 0; i--) {
                if (j >= 0 && word1.charAt(i) == word2.charAt(j)) {
                    j--;
                }
                suf[i] = j + 1;
            }
    
            int[] ans = new int[n];
            int size = 0;
            boolean changed = false;
            j = 0;
    
            for (int i = 0; i < m; i++) {
                char c = word1.charAt(i);
                if (c == word2.charAt(j) || (!changed && suf[i + 1] <= j + 1)) {
                    if (c != word2.charAt(j)) {
                        changed = true;
                    }
                    ans[size++] = i;
                    j++;
                    if (j == n) {
                        return ans;
                    }
                }
            }
    
            return new int[0];
        }
    }
    
  • class Solution {
    public:
        vector<int> validSequence(string word1, string word2) {
            int m = word1.size(), n = word2.size();
    
            vector<int> suf(m + 1);
            suf[m] = n;
    
            int j = n - 1;
            for (int i = m - 1; i >= 0; i--) {
                if (j >= 0 && word1[i] == word2[j]) {
                    j--;
                }
                suf[i] = j + 1;
            }
    
            vector<int> ans;
            bool changed = false;
            j = 0;
    
            for (int i = 0; i < m; i++) {
                char c = word1[i];
                if (c == word2[j] || (!changed && suf[i + 1] <= j + 1)) {
                    if (c != word2[j]) {
                        changed = true;
                    }
                    ans.push_back(i);
                    j++;
    
                    if (j == n) {
                        return ans;
                    }
                }
            }
    
            return {};
        }
    };
    
  • class Solution:
        def validSequence(self, word1: str, word2: str) -> List[int]:
            m, n = len(word1), len(word2)
            suf = [0] * (m + 1)
            suf[m] = n
            j = n - 1
            for i in range(m - 1, -1, -1):
                if j >= 0 and word1[i] == word2[j]:
                    j -= 1
                suf[i] = j + 1
    
            ans = []
            changed = False
            j = 0
            for i, c in enumerate(word1):
                if c == word2[j] or (not changed and suf[i + 1] <= j + 1):
                    if c != word2[j]:
                        changed = True
                    ans.append(i)
                    j += 1
                    if j == n:
                        return ans
            return []
    
    
  • func validSequence(word1 string, word2 string) []int {
    	m, n := len(word1), len(word2)
    
    	suf := make([]int, m+1)
    	suf[m] = n
    
    	j := n - 1
    	for i := m - 1; i >= 0; i-- {
    		if j >= 0 && word1[i] == word2[j] {
    			j--
    		}
    		suf[i] = j + 1
    	}
    
    	ans := make([]int, 0, n)
    	changed := false
    	j = 0
    
    	for i := 0; i < m; i++ {
    		c := word1[i]
    		if c == word2[j] || (!changed && suf[i+1] <= j+1) {
    			if c != word2[j] {
    				changed = true
    			}
    			ans = append(ans, i)
    			j++
    
    			if j == n {
    				return ans
    			}
    		}
    	}
    
    	return []int{}
    }
    
    
  • function validSequence(word1: string, word2: string): number[] {
        const m = word1.length;
        const n = word2.length;
    
        const suf = new Array<number>(m + 1).fill(0);
        suf[m] = n;
    
        let j = n - 1;
        for (let i = m - 1; i >= 0; i--) {
            if (j >= 0 && word1[i] === word2[j]) {
                j--;
            }
            suf[i] = j + 1;
        }
    
        const ans: number[] = [];
        let changed = false;
        j = 0;
    
        for (let i = 0; i < m; i++) {
            const c = word1[i];
    
            if (c === word2[j] || (!changed && suf[i + 1] <= j + 1)) {
                if (c !== word2[j]) {
                    changed = true;
                }
    
                ans.push(i);
                j++;
    
                if (j === n) {
                    return ans;
                }
            }
        }
    
        return [];
    }
    
    

All Problems

All Solutions