Find the different character in two strings

  • 0

    Given string s and t, one string has one character that is not contained by another string. Find this character.

    s = "aabccdeef";
    t = "cEaXeFdBca";

    X in the t is the character that is not contained by s

  • 0

    This is my solution. Generally, calculate the frequency of each Character in both s and t. Then compare in two way to see is there any character existing in one, but not in another regardless of upper or lower case. Any comments are welcome.

    public static void main(String[] args) {
    	String s = "aabccdeef";
    	String t = "cEaXeFdBca";
    	Map<Character, Integer> sFrq = getFrq(s);
    	Map<Character, Integer> tFrq = getFrq(t);
    	char diff = findDiff(sFrq, tFrq);
    	if (findDiff(sFrq, tFrq) != ' ' || findDiff(tFrq, sFrq) != ' ') {
    	else {
    		System.out.println("There is no difference.");
    private static char findDiff(Map<Character, Integer> srcFrq, Map<Character, Integer> dstFrq) {
    	for (Character c : dstFrq.keySet()) {
    		if (!srcFrq.containsKey(Character.toUpperCase(c)) 
    				&& !srcFrq.containsKey(Character.toLowerCase(c))) {
    			return c;
    	return ' ';
    private static Map<Character, Integer> getFrq(String s) {
    	Map<Character, Integer> frequencies = new HashMap<Character, Integer>();
    	for (Character c : s.toCharArray()) {
    		if (!frequencies.containsKey(c)) {
    			frequencies.put(c, 1);
    		else {
    			frequencies.put(c, frequencies.get(c) + 1);
    	return frequencies;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.