import java.io.*;
/**
 * StringTrivia reports various facts about Strings
 */
public class StringTrivia {
  
  /**
   * Return whether the number of vowels is greater than the number of
   * consonants in s.  Consider vowels to be the characters contained in
   * the String "aeiouyAEIOUY", and consonants to be the alphabetic
   * characters that are NOT in that String.
   * Precondition: s != null
   * @param s String to compare number of vowels and consonants in.
   * @returns Whether the number of vowels in s is greater than the number of consonants.
   */ 
  public static boolean vowelsBeatConsonants(String s) {
    int vowelCount= 0, consonantCount= 0;
    for (int pos= 0; pos < s.length(); pos++) {
      if ("AEIOUYaeiouy".indexOf(s.charAt(pos)) > -1) {
        vowelCount++;
      }
      else if ("BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz".
                 indexOf(s.charAt(pos)) > -1) { // must be a consonant
        consonantCount++;
      }
    }
    return vowelCount > consonantCount;
  }
  
  /**
   * Return whether s can be formed from t by deleting (but not
   * re-ordering) some of t's characters.  For example,
   * "abc" can be formed from "RaTbbNcQ" by deleting R, T, b, N,
   * and Q.  Notice that if s is the empty String ("") this true
   * for EVERY t!
   * Precondition: s != null && t != null
   * @param s Possible subsequence of t
   * @param t Possible supersequence of s
   * @returns Whether s is a subsequence of t.
   */
  public static boolean isSubsequence(String s, String t)  {
    boolean isSubsequenceSoFar= true; // until shown to be false
    int charIndex= 0;
    for (int pos= 0; pos != s.length() && isSubsequenceSoFar; pos++) {
      if (t.indexOf(s.charAt(pos), charIndex) == -1) {
        isSubsequenceSoFar= false;
      }
      else {
        charIndex= t.indexOf(s.charAt(pos), charIndex);
      }
    }
    return isSubsequenceSoFar;
  }
  
  /**
 * Return the average length of the lines in file f.
 * Precondition: f != null and the file is not empty.
 * @param f Name of file to read Strings from
 * @return Average line length
 */
  public static double averageStringLength(String f) throws IOException {
    int numLines= 0, lengthTally= 0;
    BufferedReader br= new BufferedReader(new FileReader(f));
    for (String input= br.readLine(); input != null; input= br.readLine()) {
      lengthTally += input.length();
      numLines++;
    }
    br.close(); // tidy up
    return (1.0 * lengthTally) / numLines; // denominator at least 1 since f no empty
  }

/**
 * Return the number of non-overlapping pairs of characters in s.
 * For example "ooo" contains just a single non-overlapping pair,
 * whereas "bookkeeper" contains three non-overlapping pairs.
 * Precondition: s != null
 * @param s String to inspect for non-overlapping pairs of characters
 * @return Number of non-overlapping pairs of characters.
 */
  public static int doubleLetters(String s) {
    int pairCount= 0, pos= 1; // start at second character, if it exists, in s
    while (pos < s.length())  {
      if (s.charAt(pos) == s.charAt(pos - 1)) {
        pairCount++;
        pos++; // skip next position if a pair is found
      }
      pos++; // in every case move ahead
    }
    return pairCount;
  }
  
}
