Java Collections | Set 8 (TreeSet) | Practice | GeeksforGeeks

Given n strings added in a TreeSet and four characters a,b,c,d. Firstly print all the strings which are less than a , then print all the strings which are greater than or equal to b. In the end print all the strings which are greater than or equal to c and less than d i.e between c and d ( excluding d ). 
Input:
The first line of input is T denoting the number of testcases. First line consists of an integer n. Next line contains n spaced strings. Last line of each test case consists of 4 space separated characters.
Output:
For each test case output will be in three lines:
First Line :  Print all the strings which are less than a (char variable).
Second Line : Print all the strings which are greater than or equal to b (char variable).
Third Line :  Print all the strings which are greater than or equal to c and less than d i.e between c and d ( excluding d ). 
Constraints:
1<=T<=100
1<=N<=100
1<=Length of string<=100
Example:
Input:
1
3
sa ka da
s k c s
Output:
[da, ka]
[ka, sa]
[da, ka]
0
hitentandon3 months ago
System.out.println(ts.headSet(a+""));
      System.out.println(ts.tailSet(b+""));
      System.out.println(ts.subSet(c+"",d+""));
+1
rajat25gupta3 months ago
Correct Java Solution
 
void task(TreeSet<String> ts,char a,char b,char c,char d)   {       // Add your code here. Print the output here itself.       System.out.println(ts.headSet(Character.toString(a)));       System.out.println(ts.tailSet(Character.toString(b)));       System.out.println(ts.subSet(Character.toString(c), Character.toString(d)));   }
0
rr71951424 months ago
Correct AnswerExecution Time:0.3 sec
//---CODE IN JAVA---
 TreeSet<String> ts1 = new TreeSet<>();       TreeSet<String> ts2 = new TreeSet<>();       TreeSet<String> ts3 = new TreeSet<>();       java.util.Iterator<String> it = ts.iterator();       while(it.hasNext()){           String str = it.next().toString();           char cc = str.charAt(0);           if(cc<a){               ts1.add(str);           }           if(cc>=b){               ts2.add(str);           }           if(cc>=c && cc<d){               ts3.add(str);           }       }       System.out.println(ts1);       System.out.println(ts2);       System.out.println(ts3);
0
Aaditya Burujwale1 year ago
Aaditya Burujwale
Java Solution Execution Time - 0.42 Sec 
0
Amir Ansari1 year ago
Amir Ansari
Correct AnswerExecution Time:0.49
      void task(TreeSet<string> ts,char a,char b,char c,char d)    {        // Add your code here. Print the output here itself.         java.util.Set<string> setA = new java.util.TreeSet<>();  java.util.Set<string> setB = new java.util.TreeSet<>(); java.util.Set<string> setC = new java.util.TreeSet<>();
java.util.Iterator it = ts.iterator();
while(it.hasNext()) {     String str = it.next().toString();   char ch = str.charAt(0);         if(ch < a)       setA.add(str);
      if(ch >= b)       setB.add(str);
      if(ch >= c && ch <d) setc.add(str);="" }="" system.out.println(seta);="" system.out.println(setb);="" system.out.println(setc);="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.