0

I have string array like this:

String[] s={"0010", "0110", "1010", "1110", "0001", "0011"}

I need to get each pairs of elements which changed inside array. In this case, only one element out of 4 gets changed for every string.

For the example: for 0010, as the first position(0) change to 1 on 1010, I need to print out

{"0010","1010"}

for 0001, as the third position(0) change to 1 on 0011, I need to print out

{"0001", "0011"}

and so on. Is there any way in java to get this result and how? Is this same to get string inside string array?

6
  • 4
    Are those supposed to be Strings? As posted, your code line doesn't compile. Also, can you elaborate a bit, maybe show us what you have tried yourself? Commented Feb 24, 2015 at 11:38
  • That's not valid code to start with - 0010 isn't a string... It really helps if you provide a short but complete program which actually demonstrates the problem, rather than just describing it. Commented Feb 24, 2015 at 11:38
  • String[] s={"0010", "0110","1010","1110"," 0001"," 0011"} . I think this is the valid String Array Commented Feb 24, 2015 at 11:43
  • Ah yes, I'm forget "" while typing. I edited my question. Commented Feb 24, 2015 at 11:46
  • 1
    is this like only one element out of 4 gets changed ? please explain the precise defination for a set here. Commented Feb 24, 2015 at 11:53

1 Answer 1

2
public class Test {
public static void main(String args[])
{
    String[] s={"0010", "0110", "1010", "1110", "0001", "0011"};
    for(int i=0;i<s.length;i++)
    {
        for(int j=i+1;j<s.length;j++)
        {
            if(isPair(s[i].toCharArray(), s[j].toCharArray()))
            {
                System.out.println("{\""+s[i] + "\",\"" + s[j]+"\"}");
            }
        }
    }
}
public static boolean isPair(char[] a, char[] b)
{
    int count = 0;
    if(a.length == b.length)
    {
        for(int i=0;i<a.length;i++)
        {
            if(a[i]!=b[i])
                count++;
        }
    }
    return count == 1 ? true : false;
}
}
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, it works! So I need to compare like this, I see. Thank you @anurag gupta

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.