Niuke - 2016 - Where to Find Coder

Posted by iJoseph on Thu, 13 Jun 2019 22:27:02 +0200

Rabbit didn't know until noon today that most of the Japanese opera resources in my big station B were blocked yesterday. It's a frog boiled in warm water. Looking at him from a tall building, looking at his collapse, maybe I will have to chat with the customer service application to help me convey yo....


Title:
Design an efficient algorithm to find a string containing "Coder" in a given string array (case-insensitive) and return it as a new array. As a result, the sequence of strings decreases according to the number of occurrences of "Coder". If the occurrences of "Coder" in the two strings are the same, their position relationship in the original array is maintained.
Given a string array A and its size n, return the result array. Ensure that the size of the original array is less than or equal to 300, where the length of each string is less than or equal to 200. At the same time, it is guaranteed that there must be a string containing coder.
Input:
["i am a coder","Coder Coder","Code"],3
Output:
["Coder Coder","i am a coder"]
Analysis:
1. It is case-insensitive, so all data can be converted to a uniform lowercase or capitalization
2. How to determine whether or not the string is included and how many strings are included
3. Sorting results, priority counts, and source order when counts are the same
Functions needed to solve the problem:
1. Convert case to case

// Determine whether it is lowercaseorIs it capitalized?
Character.isLowerCase(c); 
Character.isUpperCase(c);
// Convert to capitalizationorA lowercase letter 
Character.toLowerCase(c);
Character.toUpperCase(c);

2. Determine whether a string is included

// Determine whether coder is included or not.ifUse
a.contains("coder")

3. There are four methods to find neutron strings in Java

String a="I can abc";
// 1 Returns the index indexOf(String str) of the specified substring that appears for the first time in this string.
int k = a.indexOf("abc");
// 2 Starting at the specified index, returns the index indexOf (String str, int start index) of the first occurrence of the specified substring in the string.
int k = a.indexOf("abc",0);
// 3 Returns the index lastIndexOf(String str) of the specified substring appearing on the rightmost side of the string 
int k = a.lastIndexOf("abc");
// 4 Searches backwards from the specified index, returning the index lastIndexOf (String str, int start index) for the last occurrence of the specified substring in this string.
int start=10;
int k = a.lastIndexOf("abc",start);

4. Array sorting. Collections.sort uses merge sorting. To use it, you need to implement the Comparator interface manually and override the compare method.
Special attention should be paid to reducing the number of occurrences and increasing the order of index in the original array, and arranging first and then indexing.
Back-front-big in front; front-back-small in front

//Merge and sort List s using Collections
//Need to implement its Comparator interface parameters
Collections.sort(result, new Comparator<Recorder>(){
    @Override
        public int compare(Recorder o1, Recorder o2){
        //First of all, the number of "coder s" is higher than the number of count s.
        if(o1.getCount() != o2.getCount())
            return o2.getCount() - o1.getCount();
        //Compared with index, index is the smallest.
        else{
            return o1.getIndex() - o2.getIndex();
        }
    }
});

5. Create arrays of custom classes

// We've all used ArrayList
ArrayList<String> List = new ArrayList<String>(); 
// The Recorder here is similar to String. Don't forget to set getter and setter in the class Recorder.
ArrayList<Recorder> result = new ArrayList<Recorder>();

Code:

import java.util.*;

public class Coder {
    // Known string A and string size n
    public String[] findCoder(String[] A, int n) {
        // write code here
        // Create an array to save the results
        ArrayList<Recorder> result = new ArrayList<Recorder>();
        // for loop traverses String array A
        for(int i=0;i<n;i++){
            // All converted to lowercase
            String a = A[i].toLowerCase();
            // Determine whether coder is included
            if(a.contains("coder")){
                int count = 0;
                int start = 0;
                // Looking for coder, starting with start, I think regular expressions are better. Look at them another day.
                while(a.indexOf("coder", start)>=0 && start < a.length()){
                    count++;
                    start = a.indexOf("coder", start) + "coder".length();
                }
                result.add(new Recorder(A[i], i, count));
            }
        }
        //Merge and sort List s using Collections
        //Need to implement its Comparator interface parameters
        Collections.sort(result, new Comparator<Recorder>(){
            @Override
                public int compare(Recorder o1, Recorder o2){
                //First of all, the number of "coder s" is higher than the number of count s.
                if(o1.getCount() != o2.getCount())
                    return o2.getCount() - o1.getCount();
                //Compared with index, index is the smallest.
                else{
                    return o1.getIndex() - o2.getIndex();
                }
            }
        });
        // Initialization data
        String sorted[] = new String[result.size()];
        for(int i=0;i<result.size();i++){
            String s = result.get(i).getData();
            sorted[i] = s;
        }
        return sorted;
    }
    //Custom Categories
    class Recorder{
        // String + position + how many?
        private String data;
        private int index;
        private int count;
        // Custom initialization function
        public Recorder(String data,int index,int count){
            this.data=data;
            this.index=index;
            this.count=count;
        }
        // getter establishment
        public String getData(){
            return data;
        }
        public int getIndex(){
            return index;
        }
        public int getCount(){
            return count;
        }
    }
}

Topics: less Java