Not Passing Some test cases

import java.util.*;
public class Main {
public static String SheldonUniqueString(String str)
{
int[] lastInd=new int[26];
Stack stk=new Stack<>();
for(int i=0;i<str.length();i++){
lastInd[str.charAt(i)-‘a’]=i;
}
for(int i=0;i<str.length();i++)
{
if(!stk.isEmpty())
{
if(stk.contains(str.charAt(i)))
{
continue;
}
while(!stk.isEmpty() && stk.peek()>str.charAt(i) && lastInd[stk.peek()-‘a’]>lastInd[str.charAt(i)-‘a’] )
{
stk.pop();
}
}
stk.push(str.charAt(i));
}
StringBuilder stb=new StringBuilder();
while(!stk.isEmpty()){
stb.append(stk.pop());
}
return stb.reverse().toString();
}
public static void main (String args[]) {
Scanner scn=new Scanner(System.in);
while(scn.hasNext()){
System.out.println(SheldonUniqueString(scn.next()));
}

}

}

here last condition should have been lastInd[stk.peek()-‘a’]>i as you just have to see whether current popped element can come later or not.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

Hi Mayank,
Yes my doubt got resolved
Thank you for your help