Java simple solution O(n log n)

  • 1

    Sorting both Strings and then going through the shorter one to see if there is any difference. If the difference has been found return the character, else the difference is the last character on t.

    public class Solution {
        public char findTheDifference(String s, String t) {
            char[] s1 = s.toCharArray();
            char[] t1 = t.toCharArray();
            for (int i = 0; i < s1.length; i++) {
                if (t1[i] != s1[i]) return t1[i];
            return t1[t1.length -1];

Log in to reply

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