O(m+n) solution


  • 0
    W

    Just take O(m+n)

    public class Solution {
        public char findTheDifference(String s, String t) {
            if (s == null || t == null) return ' ';
            int[] index = new int[26];
            char[] ss = s.toCharArray();
            int sLen = ss.length;
            char[] tt = t.toCharArray();
            int tLen = tt.length;
            for (int i = 0; i < sLen; i++) {
                index[ss[i] - 'a']++;
            }
            for (int i = 0; i < tLen; i++) {
                index[tt[i] - 'a']--;
            }
            for (int i = 0; i < 26; i++) {
                if (index[i] < 0) return (char) (i + 'a');
            }
            return ' ';
        }
    }
    

Log in to reply
 

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