In the problem String Sort the first test case is passed but the second test case is failing. Here is my code
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String [] strArr = new String[n];
for(int i=0; i<n; i++)
{
String s = sc.next();
strArr[i] = s;
}
for(int i=0; i<n; i++)
{
for(int j=0; j<n-1-i; j++)
{
if (strArr[j].compareTo(strArr[j+1])>0)
{
String temp = strArr[j];
strArr[j] = strArr[j+1];
strArr[j+1] = temp;
break;
}
}
}
for(int j=0; j<strArr.length; j++)
{
for(int i=0; i<strArr.length-1; i++)
{
int len1 = strArr[i].length();
int len2 = strArr[i+1].length();
if((len2-len1>0) && (strArr[i+1].substring(0, len1).equals(strArr[i])))
{
String temp = strArr[i+1];
strArr[i+1] = strArr[i];
strArr[i] = temp;
}
}
}
for(int i=0; i<strArr.length; i++)
{
System.out.println(strArr[i]);
}
}
}