Allan Tanaka
Allan Tanaka

Reputation: 307

Searching char arrays to see if the char matches

I have two char arrays that takes two strings as the input. If the char on either side has the matching char, for example after translating the char Arrays into string char A and B both have at least one H or R, then it will return true. If not then return false.

>char[] A = foo(A).toCharArray();
>
>char[] B = foo(B).toCharArray();
>
>System.out.println("String "+A+": "+Arrays.toString(A));
>
>System.out.println("String "+B+": "+Arrays.toString(B));

>String A: [H,  , R,  ]
>
>String B: [R,  , R, R]
>>This will return true

>String A: [ , H,  , R]
>
>String B: [H, H,  , H]
>>This will return true

>String A: [H,  , H,  ]
>
>String B: [R,  , R,  ]
>>This will return false

I'm confused how to make such rule?

Upvotes: 1

Views: 107

Answers (5)

MS90
MS90

Reputation: 1249

Using Java 1.8 you could do something like following:

//@Momir Sarac
String text1 = "b9H ello";
String text2 ="avn1c fk";
// 32 is for empty space character here, you could do i != ' ' and it would be the same
//turn text1 into intstream each int corresponding to it's char value
//filter empty space ints
//get only distinct out of them
//take a look for any match if some int is contained within text2
boolean result = text1.chars().filter(i->i != 32).distinct().anyMatch(character->text2.contains(String.valueOf(character)) || text2.lastIndexOf(character) != -1);
//print it on screen
System.out.printf("Two texts %s same letter(s).", result ? "have" : "don't have");

Upvotes: 0

Rohail Usman
Rohail Usman

Reputation: 127

Well its simple all you have to do is to add a nested loop

for(int i = 0; i < A.length; i++){
      for(int j = 0; j < B.length; j++){
             if(if B[j] ==A [i]){
                  return true
             }
      }
}

        return false;

Upvotes: 0

Ashishkumar Singh
Ashishkumar Singh

Reputation: 3600

You can use java.util.Set here which will give the result in one iteration. Using java.util.TreeSet further reduce the execution time as it eliminates duplicates

public static void main(String[] args) {
    char[] A = "HR".toCharArray();
    char[] B = "RRR".toCharArray();

    Set<Character> set = new TreeSet<>();
    boolean flag = false;
    for(char c : A) {
        set.add(c);
    }
    for(char c : B) {
        if(set.contains(c)) {
            System.out.println(true);
            flag = true;
            break;
        }
    }
    if(!flag) {
        System.out.println(false);
    }
}

Upvotes: 3

zin
zin

Reputation: 112

Use the first loop to take each element from the first array.

Use the second loop to check the first element inside the second array.

Check if the current value in the first array is equal to H or R.

If it is, check if it's in the second array and return true.

public static boolean check() {
        String s1 = "ABHEEF", s2 = "RDDFVW";
        char[] arr1 = s1.toCharArray();
        char[] arr2 = s2.toCharArray();

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if(arr1[i] == 'H' || arr1[i] == 'R') {
                    if(arr1[i] == arr2[j])
                        return true;
                }

            }
        }
        return false;
    }

Upvotes: 0

Marco Fiorito
Marco Fiorito

Reputation: 52

That you can do is use a For to iterate in the matriz and verify if the current item is 'R' or 'H'.

boolean returnedValue = false;

for(int i = 0; i< B.length; i++){
  char currentItem = B[i];
  if(currentItem == 'R' || currentItem == 'H'){
    returnedValue = true;
 }
}

return returnedValue;

Upvotes: 0

Related Questions