1. String matching has a string consisting of a combination of words and Spaces, such as “Beijing Hangzhou Hangzhou Beijing Shanghai”, which requires input of a matching pattern (simply written in characters, such as AABB) to determine whether the string matches the pattern. For example:

  1. Pattern =” abbac”, STR =” Beijing, hangzhou, Beijing, Shanghai
  2. Pattern =” aacbb”, STR =” Beijing Beijing Shanghai Hangzhou Beijing “return false
  3. Pattern =” baabcc”, STR =” Beijing hangzhou Beijing Shanghai Shanghai
public class Solution {
    public boolean wordPattern(String pattern, String str) {}}Copy the code

My answer:

package exam;

import java.util.HashMap;
import java.util.Map;

public class Solution {

    // pattern =" abbac", STR =" Beijing, hangzhou, Beijing, Shanghai
    public boolean wordPattern(String pattern, String str) {
        Map<Character, String> map = new HashMap<>();

        String[] strs = str.split("");

        if(pattern.length() ! = strs.length){return false;
        }

        int n = pattern.length();

        for(int i = 0; i < n; i ++){
            char c = pattern.charAt(i);
            String temp = strs[i];

            if(! map.containsKey(c) && ! map.containsValue(temp)){ map.put(c, temp); }else{
                if(! temp.equals(map.get(c))){return false; }}}return true;

    }

    public static void main(String[] args) {
        Solution s = new Solution();
// String pattern = "abbac";
// String STR = "Beijing, Hangzhou, Beijing, Shanghai ";
// String pattern = "aacbb";
// String STR = "Beijing Beijing Shanghai Hangzhou Beijing ";
        String pattern = "aabb";
        String str = "Beijing Beijing Beijing Beijing Beijing"; System.out.println(s.wordPattern(pattern, str)); }}Copy the code

I would like to thank qq_36922084 for reminding me that the previous version of the code could not pass all the test cases, because it did not take into account that the map would add the same values repeatedly, and these values might correspond to different keys. For example: If STR = “Beijing Beijing Beijing Beijing” and parten = “aabb”, the original code will output a true, but this error is clear, now correct, in the judge! Map.containskey (c) joins at the same time! Map.containsvalue (temp) is used to ensure that there are no duplicate key value pairs in the collection.


To protect the API interface, a flow control function needs to be established. According to the API name, a specified number of requests can be made per minute (for example, 1000 times). If the request exceeds the limit, an error will be returned in one minute, but normal requests can be made in the next minute.

public class API {

    /* Timeout time */
    private long time;

    /* Number of API requests */
    private int count;

    public long getTime(a) {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }

    public int getCount(a) {
        return count;
    }

    public void setCount(int count) {
        this.count = count;
    }

    public boolean isValid(a){
        System.out.println(System.currentTimeMillis()/1000);
        // There is no timeout
        if(System.currentTimeMillis()/1000 < time){
            if(this.count < 10) {// If the number of requests is less than 10, the request is successful.
                this.count ++;
                System.out.println("Executed successfully");
                return true;
            }else{
                System.out.println("Failed for more than 1000 requests.");
                return false; }}else{
        	// Reset when limiting traffic is triggered
            System.out.println("-- -- -- -- -- -- -- -- -- -- reset -- -- -- -- -- -- -- -- -- --");
            this.count = 1;
            this.setTime(System.currentTimeMillis()/1000 + 1);
            return true; }}}Copy the code
import javax.sound.midi.Soundbank;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/ * * * 2 *@Author: Mr. Wang * 3 *@Date: 2021/3/3 16:12
 * 4
 */
public class UseApi {
	/* * Use a map to record the name and frequency of requests */
    private static Map<String, API> map = new HashMap<>();

    public boolean invoke(String name){

        if (name.isEmpty()){
            return false;
        }

        synchronized (map){
            System.out.println(map.containsKey(name));
            // This command is requested for the first time
            if(! map.containsKey(name)){ API api =new API();
                api.setTime(System.currentTimeMillis()/1000 + 1);
                api.setCount(1);
                map.put(name, api);
                return true;
            }else{
                API api = map.get(name);
                returnapi.isValid(); }}}// Test method, the same method is requested 10 times in the thread
    public static Runnable getThread(a){
        return new Runnable() {
            @Override
            public void run(a) {
                for(int i = 0; i < 10; i ++){
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    UseApi useApi = new UseApi();
                    System.out.println(useApi.invoke("method1")); }}}; }public static void main(String[] args) {

        ExecutorService executorService = Executors.newFixedThreadPool(10);
		// Open 10 threads to test
        for (int i = 0; i < 10; i++) {
            executorService.submit(getThread());
            try {
                Thread.sleep(100);
            } catch(InterruptedException e) { e.printStackTrace(); } } executorService.shutdown(); }}Copy the code

There should be a better way to time this one minute, please share in the comments