Two sorting methods (direct judgment)

Topic description

Koala has n strings of strings, and any two strings have different lengths. Koala recently learned that there are two ways to sort strings: 1. Sort strings according to their lexicographical order. For example:
"car" < "carriage" < "cats" < "doggies < "koala"
2. Sort according to the length of the string. For example:
"car" < "cats" < "koala" < "doggies" < "carriage"
The koala wants to know whether the order of these strings satisfies these two sorting methods. The koala is busy eating leaves, so it needs your help to verify.

Enter description:

Enter the number of strings in the first line n (n ≤ 100)
The next n lines, each line has a string, the length of the string is less than 100, and it is composed of lowercase letters

Output description:

If the strings are sorted lexicographically instead of length output "lexicographically", 
if they are sorted by length instead of lexicographically output "lengths",
if both ways match output "both", otherwise output "none"
Example 1

enter

3
a
aa
bbb

output

both


 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import java.util.Scanner;
 4 
 5 public class Main {
 6 static public int n ;
 7 static public String[] strs = {"a","aa","bbb"};
 8 static public Boolean lengths = true;
 9 static public Boolean lexicographically = true;
10 static public String f() {
11     for (int i = 1; i < strs.length; i++) {
12         
13         if (lengths) {
14             if (strs[i].length()<strs[i-1].length()) {
15                 lengths = false;
16             }
17         }
18         if (lexicographically) {
19             if (strs[i].compareTo(strs[i-1])<0) {
20                 lexicographically = false;
21             }
22         }
23         if (!lengths&&!lexicographically) {
24             return "none";
25         }
26         
27     }
28     if (lengths&&lexicographically) {
29         return "both";
30     }
31     if (lengths&&!lexicographically) {
32         return "lengths";
33     }
34     if (!lengths&&lexicographically) {
35         return "lexicographically";
36     }
37     return null;
38 }
39 
40 public static void main(String[] args) {
41     Scanner sc = new Scanner(System.in);
42     n = Integer.parseInt(sc.nextLine().trim());
43     strs = new String[n];
44     for (int i = 0; i < n; i++) {
45         strs[i] = sc.nextLine();
46     }
47     
48     System.out.println(f());
49 }
50 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325133213&siteId=291194637