package eecs1022.finalc; /** * This question is worth 2.5 marks. No partial marks will be awarded for this question. */ public class Question7 { /** * Returns the length of a largest gap between two identical characters. * For example, in the string "adhbfsdgda", the substring "dhbfsdgd" * forms a gap between the two a's since all characters of the substring * are different from a. You may assume that the string is not null and * not empty. You may may also assume that all characters are lower case * letters. * * @param s a string. * @return the length of a largest gap between two identical characters. */ public static int gap(String s) { int gap = 0; for (char c = 'a'; c <= 'z'; c++) { int index = s.length(); for (int i = 0; i < s.length(); i++) { char letter = s.charAt(i); if (letter == c) { if (i - index - 1> gap) { gap = i - index - 1; } index = i; } } } return gap; } /** * Tests method. You may add more test cases. * * @param args not applicable. */ public static void main(String[] args) { System.out.println(Question7.gap("adhbfsdgda")); System.out.println(Question7.gap("ababbabbbabbbba")); System.out.println(Question7.gap("a")); System.out.println(Question7.gap("aa")); System.out.println(Question7.gap("abcba")); } }