Java O(n)

  • 0

    The program iterates all characters in t which makes it O(n) where n = t.length().
    If t does not contain an extra char, it throws an error.

    class Solution {
        public char findTheDifference(String s, String t) {
            for(char mCharT : t.toCharArray()){
                if(s.indexOf(mCharT) == -1)
                	return mCharT;
                	s = s.replaceFirst(String.valueOf(mCharT), "");
            throw new java.lang.Error("t contains same chars as s");

Log in to reply

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