389. Find the Difference. Please review my code.


  • 0
    A
    class Solution 
    {
    public:
        char findTheDifference(string s, string t) 
        {
            sort(s.begin(), s.end());
            sort(t.begin(), t.end());
            int i;
            for(i=0; i<t.size(); i++)
            {
                if(t[i]-s[i]!=0)
                {
                    return t[i];
                }
            }
            return t[i];
        }
    };
    

  • 0
    This post is deleted!

  • 0
    A

    why did you delete ? :(

    atleast, review my code?


  • 0
    T

    @axaysd You're going to need to learn early on that no one is going to praise you for being smart in this profession. That only happens when you're a kid and your parents want you to feel good about yourself. If what I just mentioned wasn't the case and you actually only wanted critique, that is literally why 99% of other people post their solutions here, so begging for praise, erm, I mean, critique is just going to make you look like a big baby.


  • 1
    A

    @tonytata noo!! i wanted to know if my solution is optimal and if I'm doing good enough. (I'm new to competitive coding.)

    I couldn't find any optimal solution in "discuss" since I was one of the first to solve the problem

    I want critique. Do you think mine's optimal? (because I think there might be better ways since mine looks naive and brute force-y)


  • 0
    T

    @axaysd To answer you, no it's not optimal. Your solution is O(nlogn)


  • 0
    A

    How about this?

    class Solution 
    {
    public:
        char findTheDifference(string s, string t) 
        {
            int sumt=0, i;
            for(i=0; i<t.size(); i++)
            {
                sumt = sumt+t[i];
            }
            for(i=0; i<s.size(); i++)
            {
                sumt = sumt - s[i];
            }
            return sumt;
        }
    };```

Log in to reply
 

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