A string is determined whether or not all the characters in the character string occurs in B

problem

Enter two strings str1 and str2, please determine whether or not all the characters in str1 are in str2

Thinking

1. The conversion into character array and str2 sort
2. str1 Each binary search characters in the character array

Code

/*
 * 判断字符串A中的字符是否全部出现在字符串B中
 */
public class 判断数组的包含问题 {
	public static void main(String[] args) {
		String s1 = "bacd";
		String s2 = "ecfbratd";
		System.out.println(isContain(s1,s2));
	}
	public static Boolean isContain(String s1,String s2) {
		char[] s2_arr = s2.toCharArray();
		Arrays.sort(s2_arr);
		for(int i = 0; i < s1.length(); i++) {
			int index = Arrays.binarySearch(s2_arr, s1.charAt(i));
			if(index == -1) {
				return false;
			}
		}
		return true;
	}

}

Published 33 original articles · won praise 3 · Views 3801

Guess you like

Origin blog.csdn.net/qq_43169220/article/details/103072854