preface

I received a notice yesterday, and I have an interview today, a medium-sized e-commerce company in Modu, whose name I will not say. Heart immediately smug, eager to try, the evening also looked at the interview questions, to prepare. After arriving at the destination, I finished the test paper first. The interviewer came slowly, and there were two of them, I guess one was the product director and the other was the technical staff. Obviously, the technical staff was the main questioner in the interview. When you come in, stare at each other for a few seconds, and the air starts to look awkward. Product leaders say you are XX, first briefly introduce yourself, and then I will blah blah blah…

At the end of the introduction, the product director was still looking through my resume, and the familiar six eyes turned into four eyes between me and the technical interviewer. The technical staff took the lead in breaking the embarrassment and proposed to play a string game, and then started to get down to business.

How do I determine how many characters are the same in two strings

The technical interviewer asks, “How do I determine if one string contains another string? How many ways are there? So, how do you determine how many characters are the same in two strings? How many ways are there?” I thought for a second and blurted out, “You can turn a string into an array of characters and loop through it, adding a count to the end of the loop to keep count. That should do the job.” I’m glad. That’s a quick response.

The first is to convert a string to an array

package com.roadway;

/**
 * @author roadway
 */
public class TestString {

    public static int judgeTwoString(String a, String b) {
        char[] aChar = a.toCharArray();
        char[] bChar = b.toCharArray();
        int r = 0;
        for(int i = 0; i < aChar.length ; i ++){for(int j = 0; j < bChar.length; j++){
                if(aChar[i] == bChar[j]) { r++; }}}return r;
    }

    public static void main(String[] args) {
        System.out.println(judgeTwoString("abcdef"."bcd")); }}Copy the code

Interviewer: “HMM, good, that’s one way to do it, that’s the first one, anything else?”

After a moment’s thought, I replied, “You can use hashMap to handle this. I remember that map has a method called getOrDefault(), which allows the caller to specify in a code statement to get a value that matches the provided key in the map, or else return a” default value “if no match is found for the provided key. That’s how many of the same elements you can count.” It’s a good thing I’ve had similar problems before, otherwise I don’t know what else to do.

The second method is the HashMap method

package com.roadway;

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

/**
 * @author roadway
 */
public class TestString {

    public static int judgeTwoString(String a, String b) {
        Map<Character, Integer> map = new HashMap<>();
        int count = 0;
        for(char c : a.toCharArray()) {
            map.put(c, map.getOrDefault(c, 0) + 1);
        }

        for(int i = 0; i < b.length(); i++) {
            count += map.getOrDefault(b.charAt(i), 0);
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(judgeTwoString("abcdef"."bcd")); }}Copy the code

The interviewer nodded and said, “It’s not very common, but it should be possible. Is there another way to do it?” … “Well… There are only two ways we can think of.” I could vaguely feel the product director next to the interviewer move. I went back to my home and found many ways. The following ways are discovered after I got home.

The third way is to compare strings directly

This is similar to the first method, but uses the charAt() method.

package com.roadway;

/**
 * @author roadway
 */
public class TestString {

    public static int judgeTwoString(String a, String b) {
        int count=0;
        for(int i=0; i<a.length(); i++){for(int j=0; j<b.length(); j++){if(b.charAt(j) == a.charAt(i)){ count+=1; }}}return count;
    }

    public static void main(String[] args) {
        System.out.println(judgeTwoString("abcdef"."bcd")); }}Copy the code

The fourth method is regular expressions

This way is awesome, I never thought of it before, there is even this kind of use, brief and clear, one line done, but this use efficiency will be lower.

package com.roadway;

/**
 * @author roadway
 */
public class TestString {

    public static int judgeTwoString(String a, String b) {
        return a.replaceAll("[^" + b + "]"."").length();
    }

    public static void main(String[] args) {
        System.out.println(judgeTwoString("abcdef"."bcd")); }}Copy the code

The fifth method is the HashSet method

package com.roadway;

import java.util.HashSet;
import java.util.Set;

/**
 * @author roadway
 */
public class TestString {

    public static int judgeTwoString(String a, String b) {
        Set<Character> set = new HashSet<>();
        for (char c : a.toCharArray()) {
            set.add(c);
        }
        int res = 0;
        for (char c : b.toCharArray()) {
            if(set.contains(c)) { res++; }}return res;
    }

    public static void main(String[] args) {
        System.out.println(judgeTwoString("abcdef"."bcd")); }}Copy the code

Here are the five more commonly used ways.

Okay, back to the conversation with the interviewer.

The interviewer deadpan: “No? I think there’s more to it than that, but okay, let’s move on to the next question. How do you determine which element is the most in a string and figure out the number?”

How do you determine which element is the most in a string and figure out the number

And the string is on the bar.

Meditating for a moment, slowly thinking of a.

I replied, “I convert the string to an array of characters, and then dump it into a HashMap collection, where the key is the number of characters in the string and the value is the number of characters in the string. You just need to find the key with the largest Value in the HashMap collection.”

The interviewer nodded and said, “That’s a great idea. Can you write it down?” He took a piece of white paper and laid it before me. Oh, my God, heartless. Handwritten code. Although I have the idea, it is really difficult for me to write purely by hand, just at this moment my hands do not obey me (actually I do not want to write). Maybe the product director saw my shaking hands and finally started working.

The product director asked directly, “Any questions about our company?” There’s a point here. “Just now also saw these two questions answer is not very good.” Thought, what ghost, so soon face over? But I smiled and said, “Is your e-commerce business omnichannel?”

Since said that still have to ask symbolically, small talk a few yun Yun, chat also ended here.

Back home to see their own distribution of the existing better implementation, as follows:

package com.roadway;

/**
 * @author roadway
 */
public class TestString {

    public static void judgeString(String str) {
        int max_length = 0;
        String max_str = "";
        while (str.length() > 0) {
            int length = str.length();
            String first = str.substring(0, 1);
            str = str.replaceAll(first, "");
            if (max_length < length - str.length()) {
                max_length = length - str.length();
                max_str = first;
            }
        }
        System.out.println(max_length + max_str);
    }

    public static void main(String[] args) {
        judgeString("abbcccddddaa"); }}Copy the code

Afterword.

Maybe because of the epidemic, many companies do not hire many people, but they still ask HR to screen resumes for interviews. I guess this is the case in this company. Basically, a lot of stuff was left unasked. It was like going through the motions.

It is not easy to change jobs during the pandemic, but there is hope, depending on how prepared you are.