884. Uncommon Words from Two Sentences

884. Uncommon Words from Two Sentences

class Solution {
    public String[] uncommonFromSentences(String A, String B) {
                List outputList = new ArrayList();  
String[] As = A.split(" ");
String[] Bs = B.split(" ");
        Map s = new HashMap();
        for (int i = 0; i < As.length; i++){
            if(s.get(As[i]) == null) s.put(As[i],1);
            else s.put(As[i],s.get(As[i])+1);
        }
        for (int i = 0; i < Bs.length; i++){
            if(s.get(Bs[i]) == null) s.put(Bs[i],1);
            else s.put(Bs[i],s.get(Bs[i])+1);
        }
        
        for (String a: s.keySet()){
            if ( s.get (a) == 1) outputList.add(a);
        }
        
        return outputList.toArray(new String[outputList.size()]);
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s