||EASY || BEGINNER  || JAVA
\n\n1. Initialization:\n   - NoOfFollowers array stores the number of characters in word1 that follow a specific character.\n   - ans array stores the indices o
\n\n1. **Initialization:**\n   - `NoOfFollowers` array stores the number of characters in `word1` that follow a specific character.\n   - `ans` array stores the indices of characters in `word1` that correspond to the characters in `word2`.\n   - `i` and `j` are initialized to the last indices of `word1` and `word2`, respectively.\n   - `num` counts the number of consecutive matching characters.\n   - `repeatOrder` indicates whether a repeated character has been skipped.\n\n2. **Finding Matching Characters:**\n   - The `while` loop iterates through `word1` and `word2` from the end.\n   - If characters match, `num` is incremented, `NoOfFollowers` is updated, and both `i` and `j` are decremented.\n   - If characters don\'t match, `NoOfFollowers` is updated, and `i` is decremented.\n\n3. **Finding Valid Subsequence:**\n   - The `while` loop iterates through `word1` and `word2` from the beginning.\n   - If characters match, the corresponding index in `ans` is updated, and both `i` and `j` are incremented.\n   - If characters don\'t match, the code checks if there are enough following characters in `word1` to match the remaining characters in `word2`. If so, the current index in `word1` is added to `ans`, `i` and `j` are incremented, and `flag` is set to `false` to indicate that a repeated character has been skipped.\n\n4. **Return Result:**\n   - If `j` is less than `n` (not all characters in `word2` were matched), an empty array is returned.\n   - Otherwise, the `ans` array containing the indices of the valid subsequence is returned.\n\n\n\n\n# Code\n```java []\nclass Solution {\n    public int[] validSequence(String word1, String word2) {\n        int m=word1.length();\n        int n=word2.length();\n        int [] NoOfFollowers=new int [m];\n        int [] ans=new int [n];\n        int i=m-1;\n        int j=n-1;\n        int num=0;\n        int repeatOrder=0;\n        while(i>=0 && j>=0){\n            if(word1.charAt(i)==word2.charAt(j)){\n                num++;\n                NoOfFollowers[i]=num;\n                i--;\n                j--;\n            }\n            else{\n                NoOfFollowers[i]=num;\n                i--;\n            }\n        }\n        while(i>=0){\n            NoOfFollowers[i]=num;\n            i--;\n        }\n\n        i=0;\n        j=0;\n        boolean flag=true;\n        while(i<m && j<n){\n            if(word1.charAt(i)==word2.charAt(j)){\n                ans[j]=i;\n                i++;\n                j++;\n            }\n            else if(word1.charAt(i)!=word2.charAt(j) && (flag && (i<m-1 && NoOfFollowers[i+1]>=(n-j-1)))) {\n                ans[j]=i;\n                i++;\n                j++;\n                flag=false;\n            }\n            else{\n                i++;\n            }\n        }\n        if(j<n){\n            int [] arr={};\n            return arr;\n        }\n        return ans;\n\n    }\n}\n```
['Greedy', 'Java']