-1
\$\begingroup\$

I believe I have the code right for this particular question already but I do have some follow up questions. I'm still fairly new to this and I got it to spit out what I was looking for. If there are room for improvements I am open to criticisms. I'm just looking to get better.

How would I approach this differently or how would I solve it if the 2 given Strings are VERY large and the memory is limited?

import java.util.ArrayList;

public class DifferChar {

public char diffChar(String str1, String str2)
{
ArrayList<Character> al1 = new ArrayList<Character>();
ArrayList<Character> al2 = new ArrayList<Character>();
String longer;
String shorter;
char c = '\u0000';

if (str1 != null && str2 != null)
{
    if (str1.length() > str2.length())
    {
        longer = str1.toUpperCase();
        shorter = str2.toUpperCase();
    }
    else
    {
        longer = str2.toUpperCase();
        shorter = str1.toUpperCase();
    }

    if (longer.length() - shorter.length() <= 1)
    {
        for (char ch : shorter.toCharArray())
            al1.add(ch);
        for (char ch : longer.toCharArray())
            al2.add(ch);
        for (int i = al1.size()-1; i >= 0; i--)
        {
            if (al2.contains(al1.get(i)))
                al2.remove(al1.get(i));
        }
        if (al2.size() == 1)
            c = al2.get(0);
    }
}
return c;
}

public static void main(String[] args)
{
String str1 = "aklwejr";
String str2 = "aklwej";

DifferChar diff = new DifferChar();
System.out.println(diff.diffChar(str1, str2));
}
}
New contributor
noredlac is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$

put on hold as unclear what you're asking by Mast, Gerrit0, 200_success, Zeta, vnp Jan 13 at 21:32

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 8
    \$\begingroup\$ Could you clarify exactly what the requirements of the task are? \$\endgroup\$ – 200_success Jan 11 at 19:11

Browse other questions tagged or ask your own question.