[Notes for brushing questions] Niuke.com: Delete public characters

1. Description of the topic

insert image description here

2. Topic analysis

The specific idea is as follows:
Predefine String result = "" as the output result
1. Traverse each character in the str2 string and store it in HashMap (where the key is a single character, and the value is the number of occurrences of this character) 2.
Traverse For each character in the str1 string, str1 is compared with the hashmap. If the value returned by hashmap.get(character x) is null,
this string is added to the result output result
. 3. Output result

3. Detailed code implementation

public static void main(String[] args) {
    
    
		Scanner scanner = new Scanner(System.in);
		String str1 = scanner.nextLine();
		String str2 = scanner.nextLine();
		String result = "";

		Map<Character, Integer> map = new HashMap<>();
		for (int i = 0; i < str2.length(); i++) {
    
    
			if (map.get(str2.charAt(i)) == null) {
    
    
				map.put(str2.charAt(i), 1);
			} else {
    
    
				map.put(str2.charAt(i), map.get(str2.charAt(i)) + 1);
			}
		}

		for (int i = 0; i < str1.length(); i++) {
    
    
			if (map.get(str1.charAt(i)) == null) {
    
    
				result += str1.charAt(i);
			}
		}

		System.out.println(result);
	}

Guess you like

Origin blog.csdn.net/weixin_43950588/article/details/131288680