Using greedy
\njava []\nclass Solution {\n    public int[] validSequence(String word1, String word2) {\n        int n=word1.length();\n        int m=word2.length();\n       
\n```java []\nclass Solution {\n    public int[] validSequence(String word1, String word2) {\n        int n=word1.length();\n        int m=word2.length();\n        int[]g=new int[m+1];\n        Arrays.fill(g,-1);\n        int r=n-1;\n        g[m]=n;\n        int[]ans=new int[word2.length()];\n        for(int i=m-1;i>=0;i--){\n           \n            while(r>=0&&word2.charAt(i)!=word1.charAt(r)){\n                r--;\n            }\n           \n            g[i]=r;\n              if(r<0)break;\n            r--;\n        }\n        r=0;\n        int k=0;\n        boolean used=false;\n        for(int i=0;i<word1.length();i++){\n            if(r>=m)break;\n            if(word1.charAt(i)==word2.charAt(r)){\n            //    System.out.println(r+" "+i);\n                ans[k++]=i;\n                r++;    \n             //   System.out.println(r+" "+i);\n                continue;\n            }\n            if(!used&&g[r+1]>=i+1){\n                    used=true;\n                  //  System.out.println(r+" "+i);\n                    ans[k++]=i;\n                    r++;\n                    continue;\n            }\n           // if(r>=m)break;\n        }\n        if(k<m) return new int[]{};\n        return ans;\n\n\n    }\n}\n```
['Java']