Requirement: Use java to write a program and define a method that receives two strings and returns the same characters in the two strings.

♥Today is July 30, 2023, the weather is sunny and the temperature is 39℃!

I accidentally saw such a requirement: use java to write a program and define a method that receives two strings and returns the same characters in the two strings.

If you put a question like this in the basic stage of learning, you may be able to do it without thinking. Now I feel that the basics have been returned to the teacher, and I have to think about it for a while. I used two methods to do it. Please take a look. , a reminder, if it is an interview question, which method is the optimal solution, or are there other optimal solutions?

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

/**
 * @ClassName AppTest
 * @Description 创建一个类,该类中有1个方法,接收2个字符串,
 *               返回两个字符串中的相同字符(利用单利设计模式,保证该类只有一个实例)
 * @Author BlackCheng
 * @Date 2023/7/30 20:42
 * @Version 1.0
 */
public class AppTest {
    public static void main(String[] args) {
        MyUtil myUtil = MyUtil.getMyUtil();
        String str = "abc";
        String str2 = "acf";
        Collection commonCharacters = myUtil.findCommonCharacters(str, str2);
        System.out.println("commonCharacters = " + commonCharacters);
        String sameChar = myUtil.findSameChar(str, str2);
        System.out.println("sameChar = " + sameChar);
    }
}

/**
    该类是单利设计模式(饿汉式)设计
*/
class MyUtils {

    private static volatile MyUtils instance;
    private MyUtils() {

    }
    // 提供对外创建对象的方法
    public static MyUtils getInstance() {
        if(instance== null) {
            synchronized (MyUtils.class) {
                if(instance== null) {
                    instance= new MyUtils();
                }
            }

        }
        return instance;
    }


    /**
     * 接收2个字符串,返回两个字符串中的相同字符
     * ♥♥♥♥♥♥♥♥♥方式一♥♥♥♥♥♥♥♥♥
     * 解题思路: 1、首先将接收到的2个字符串分别转换成字符数组
     *           2、创建2个集合,分别遍历两个字符数组,然后将遍历的元素分别添加到集合中
     *           3、利用集合的retailAll()方法获得两个集合中的交集
     * @param str1
     * @param str2
     * @return
     */
    public Collection findCommonCharacters(String str1, String str2) {
        char[] chars = str1.toCharArray();
        char[] chars1 = str2.toCharArray();
        List<Character> list = new ArrayList<>();
        List<Character> list1 = new ArrayList<>();

        for (char aChar : chars) {
            list.add(aChar);
        }

        for (char bChar : chars1) {
            list1.add(bChar);
        }
        list.retainAll(list1);
        return list;
    }

    /**
     *接收2个字符串,返回两个字符串中的相同字符
     * ♥♥♥♥♥♥♥♥♥方式二♥♥♥♥♥♥♥♥♥
     * 解题思路: 1、创建可变字符串StringBuild
     *           2、遍历接收到的其中的任意一个字符串
     *           3、利用charAt()方法获取到字符串中的每一个字符
     *           4、通过indexof()方法判断取到的每个字符是否存在于另一个字符串中
     *
     * @param str1
     * @param str2
     * @return
     */
    public String findSameChar(String str1, String str2) {
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < str1.length(); i++) {
            char c = str1.charAt(i);
            // indexOf():返回字符在字符串中出现的索引位置,如果没有查到则返回-1
            // valueOf(): 可以将所有的基本数据类型转换成String类型
            if (str2.indexOf(c) >= 0 && result.indexOf(str2.valueOf(c)) < 0) {
                result.append(c);
            }
        }
        return result.toString();
    }
}

♥Interested friends can leave a message in the comment area to discuss♥

Guess you like

Origin blog.csdn.net/FebruaryQ/article/details/132013300