Tuesday, July 11, 2017

LeetCode 389: Find the difference


/*
  Problem: Given 2 strings which consist of only lower case letters and letters being randomly mixed, find the odd letter
*/
public class Solution {
   public char findTheDifference(String s, String t) {
        char[] first = s.toCharArray();
        char[] second = t.toCharArray();
        Arrays.sort(first);

        Arrays.sort(second);
        for (int i = 0; i < second.length; i++) {
            if (i > first.length - 1)
                return second[i];
            if (first[i] != second[i])
                return second[i];

        }
        return '0';
    }
}

No comments:

Post a Comment