using array with O(n)


  • 0

    public class Solution {
    public char findTheDifference(String s, String t) {
    char[] sChar = s.toCharArray();
    char[] tChar = t.toCharArray();
    Arrays.sort(sChar);
    Arrays.sort(tChar);
    char res = 'a';
    for (int i = 0; i < tChar.length; i ++)
    {
    if (i == tChar.length - 1)
    {
    res = tChar[i];
    }
    else if (tChar[i] != sChar[i])
    {
    res = tChar[i];
    break;
    }
    }
    return res;
    }
    }


Log in to reply
 

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