You should not use any other String methods apart from length() and charAt(), or the Java binarySearch method.
boolean lessThan(String s, String t, int n)
This method should return true if the first n elements of the array s come before the first n elements of the array t in dictionary order. For example, if n is 4:
- lessThan("binary", "bind", 4) is true, because the first four characters differ, and 'a' is less than 'd'.
- lessThan("binder", "binding", 4) is false, because the first four characters match.
- lessThan("binding", "binder", 4) is false, because the first four characters match.
- lessThan("bin", "binary", 4) is true.
- lessThan("bit", "binary", 4) is false.
What I've done so far:
which only fulfils when first 4 character match outputting false, but ive got no clue on how to implement the 1,4,5th bullet points.public boolean lessThan(String s, String t, int n) { boolean lessThan = false; for (int i =0; i<n; i++){ if (s.charAt(i) == t.charAt(i)){ lessThan = false; }else{ lessThan = true; } }return lessThan; }