anonymous
BAN USER- 1of 1 vote
Answersprint all the characters present in the given string only once in a reverse order. Time & Space complexity should not be more than O(N).
- anonymous in United States
e.g.
1)Given a string aabdceaaabbbcd
the output should be - dcbae
2)Sample String - aaaabbcddddccbbdaaeee
Output - eadbc
3)I/P - aaafffcccddaabbeeddhhhaaabbccddaaaa
O/P - adcbhef
Answer :
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Scanner;
import java.util.Set;
public class StringQAmazon {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String inputStr = sc.nextLine();
System.out.println(stringManipulation(inputStr));
}
static String stringManipulation(String str) {
if(str.isEmpty())
return "";
else if(str.length()==1)
return str;
else {
str.toLowerCase();
StringBuilder strBuilder = new StringBuilder();
strBuilder.append(str);
strBuilder.reverse();
Set<Character> set = new LinkedHashSet<Character>();
for(int i =0; i<strBuilder.length(); i++){
set.add(strBuilder.charAt(i));
}
Iterator<Character> iter = set.iterator();
strBuilder=new StringBuilder();
while(iter.hasNext()){
strBuilder.append(iter.next());
}
return strBuilder.toString();
}
//return null;
}
}| Report Duplicate | Flag | PURGE
Amazon SDE-2 String Manipulation
Repcarlawbartlett, Accountant at ASU
Managed a small team managing toy elephants for the underprivileged. A real dynamo when it comes to managing vashikaran mantra ...