View Code of Problem 22

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		while(sc.hasNext()) {
			String a = sc.next();
			String b = sc.next();
			char[] ac = a.toCharArray();
			char[] bc = b.toCharArray();
			ArrayList<Character> list = new ArrayList<>();
			for(int i = 0; i < ac.length; i ++) {
				for(int j = 0 ; j < bc.length; j ++) {
					if(ac[i] == bc[j]) {
						list.add(ac[i]);
						bc[j] = '0';
						break;
					}
				}
			}
			Collections.sort(list);
			for(int i = 0; i < list.size(); i ++) {
				System.out.print(list.get(i));
			}
			System.out.println();
		}
	}

}

Double click to view unformatted code.


Back to problem 22