Java Code with simple explanation✅
Approach\n1. Reverse Traversal:\n   - Start from the end of word1 and word2. Create an indices array to record how many characters from word2 can match starting
## Approach\n1. **Reverse Traversal**:\n   - Start from the end of `word1` and `word2`. Create an `indices` array to record how many characters from `word2` can match starting from each position in `word1`.\n\n2. **Forward Traversal**:\n   - Go through `word1` from the beginning. If a character matches with `word2`, add its index to a result list.\n   - If you can skip characters in `word2` based on the `indices` array, add that index as well.\n\n3. **Check the Result**:\n   - If we matched all characters of `word2`, convert the result list to an array and return it. If not, return an empty array.\n\n## Complexity\n- **Time complexity**: $$O(n + m)$$, where \\(n\\) is the length of `word1` and \\(m\\) is the length of `word2`.\n- **Space complexity**: $$O(n)$$ for the `indices` array.\n\n## Code\n```java\nclass Solution {\n    public int[] validSequence(String word1, String word2) {\n        int length1 = word1.length();\n        int length2 = word2.length();\n        int[] indices = new int[length1];\n        int j = length2 - 1;\n\n        // Reverse traversal to fill indices\n        for (int i = length1 - 1; i >= 0; i--) {\n            if (j >= 0 && word1.charAt(i) == word2.charAt(j)) {\n                indices[i] = (i == length1 - 1) ? 1 : indices[i + 1] + 1;\n                j--;\n            } else {\n                indices[i] = (i == length1 - 1) ? 0 : indices[i + 1];\n            }\n        }\n\n        j = 0;\n        List<Integer> result = new ArrayList<>();\n        int finalIndex = -1;\n\n        // Forward traversal to build the result\n        for (int i = 0; i < length1; i++) {\n            if (word1.charAt(i) == word2.charAt(j)) {\n                result.add(i);\n                j++;\n                if (j == length2) {\n                    break;\n                }\n            } else {\n                if ((i == length1 - 1 ? 0 : indices[i + 1]) >= length2 - j - 1) {\n                    result.add(i);\n                    j++;\n                    finalIndex = i + 1;\n                    break;\n                }\n            }\n        }\n\n        if (result.size() == length2) {\n            return convertToArray(result);\n        }\n\n        if (finalIndex == -1) {\n            return new int[0];\n        }\n\n        for (int i = finalIndex; i < length1; i++) {\n            if (word1.charAt(i) == word2.charAt(j)) {\n                result.add(i);\n                j++;\n            }\n            if (j == length2) {\n                break;\n            }\n        }\n\n        return result.size() == length2 ? convertToArray(result) : new int[0];\n    }\n\n    private int[] convertToArray(List<Integer> list) {\n        return list.stream().mapToInt(Integer::intValue).toArray();\n    }\n}\n```\n\n# Upvote if you found this helpful! \uD83D\uDE0A
['Java']