# AB Check Coderbyte Solution

0

Have the function ABCheck(str) take the str parameter being passed and return the string true if the characters a and b are separated by exactly 3 places anywhere in the string at least once(ie. "lane borrowed" would result in true because there is exactly three characters between a and b).Otherwise return the string false.

Ab Check Coderbyte Java

import java.util.*;

public class ABCheck {
String ABCheck(String str) {
char[] chars = str.toCharArray();

for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'a'&& (i + 4) < chars.length && chars[i + 4] == 'b') {
return "true";
} else if (chars[i] == 'b'&& (i + 4) < chars.length && chars[i + 4] == 'a') {
return "true";
}
}

return "false";

}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
ABCheck c = new ABCheck();
System.out.print(c.ABCheck(s.nextLine()));
}
}

# Ab Check Coderbyte Python

def ArithGeo(arr):
arith,geo = True, True
for num in range(1, len(arr) - 1):
if arr[num + 1] - arr[num] != arr[num] - arr[num-1]:
arith = False
if arr[num +1] / arr[num] != arr[num] / arr[num-1]:
geo = False
return "Arithmetic" if arith else "Geometric" if geo else -1

Ab Check coderbyte JavaScript

Using the JavaScript language, have the function ABCheck(str) take the str parameter being passed and return the string true if the characters a and b are separated by exactly 3 places anywhere in the string at least once (ie. "lane borrowed" would result in true because there are exactly three characters between a and b). Otherwise, return the string false.

I am going to use a RegExp to see if the pattern [a...b] exists anywhere in the string. If it does then return true else return false.                                                     *

Steps for solution

1) Use RegExp pattern to search string for pattern a...b

2) If found return true

3) Else return false

function ABCheck(str) {

var match = str.search(/a...b/);
if (match > -1) {
return "true";
}
else {
return "false";
}
}

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.