Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int commonTwo(String[] a, String[] b) {
- int indA = 0;
- int indB = 0;
- int count = 0;
- int temp = 0;
- String str = "";
- while(indA<a.length && indB<b.length) {
- //Skips letters previously matched
- while(str.contains(a[indA]) && indA<a.length-1) indA++;
- while(str.contains(b[indB]) && indB<b.length-1) indB++; //etc.
- // Helps test matches & decide which array position to increment
- temp = a[indA].compareTo(b[indB]);
- // If there is a match, increment both arrays,
- //count and add the matched letter to str
- if ( temp==0 ) {
- count++;
- str += a[indA];
- indA++;
- indB++;
- }
- if (temp<0) { // If a is before b, increment array A.
- indA++;
- }
- if (temp>0) { // If b is before a, increment array B.
- indB++;
- }
- }
- return count;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement