// --- Directions
// Check to see if two provided strings are anagrams of eachother.
// One string is an anagram of another if it uses the same characters
// in the same quantity. Only consider characters, not spaces
// or punctuation.  Consider capital letters to be the same as lower case
// --- Examples
//   anagrams('rail safety', 'fairy tales') --> True
//   anagrams('RAIL! SAFETY!', 'fairy tales') --> True
//   anagrams('Hi there', 'Bye there') --> False

function anagrams(stringA, stringB) {
  const aFC = buildFC(stringA);
  const bFC = buildFC(stringB);

  if (Object.keys(aFC).length !== Object.keys(bFC).length) {
    return false;
  }

  for (let char in aFC) {
    if (aFC[char] !== bFC[char]) {
      return false;
    }
  }

  return true;
}

function buildFC(str) {
  const frequencyCounter = {};

  for (let char of str.replace(/[^\w]/g, '').toLowerCase()) {
    frequencyCounter[char] = frequencyCounter[char] + 1 || 1;
  }

  return frequencyCounter;
}
// --- Directions
// Write a function that accepts a string.  The function should
// capitalize the first letter of each word in the string then
// return the capitalized string.
// --- Examples
//   capitalize('a short sentence') --> 'A Short Sentence'
//   capitalize('a lazy fox') --> 'A Lazy Fox'
//   capitalize('look, it is working!') --> 'Look, It Is Working!'
function capitalize(str) {
  let result = str[0].toUpperCase();

  for (let i = 1; i < str.length; i++) {
    if (str[i - 1] === ' ') {
      result += str[i].toUpperCase();
    } else {
      result += str[i];
    }
  }

  return result;
}
// --- Directions
// Given an array and chunk size, divide the array into many subarrays
// where each subarray is of length size
// --- Examples
// chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
// chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
// chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
// chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
// chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]

function chunk(array, size) {
  const chunked = [];
  let index = 0;

  while (index < array.length) {
    chunked.push(array.slice(index, index + size));
    index += size;
  }

  return chunked;
}

// function chunk(array, size) {
//   const chunked = [];
//
//   for (let element of array) {
//     const last = chunked[chunked.length - 1];
//
//     if (!last || last.length === size) {
//       chunked.push([element]);
//     } else {
//       last.push(element);
//     }
//   }
//
//   return chunked;
// }
// REVERSE A STRING
// Return a string in reverse
// ex. reverseString('hello') === 'olleh'

function reverseString(str){
    return str.split('').reverse().join('')
}
// VALIDATE A PALINDROME
// Return true if palindrome and false if not
// ex. isPalindrome('racecar') === 'true', isPalindrome('hello') == false

function reverseString(str){
    return str.split('').reverse().join('')
}

function isPalindrome(str){
    return reverseString(str).trim().toLowerCase() === str.trim().toLowerCase() 
}
// MAX CHARACTER
// Return the character that is most common in a string
// ex. maxCharacter('javascript') == 'a'

function maxCharacter(str){
  const fc = buildFC(str)
  let max = -Infinity
  let maxChar
  for(let key in fc){
    let lastMax = max
    max = Math.max(fc[key], max)
    if(max !== lastMax){
        maxChar = key
    }
  }

  return maxChar
 
}

function buildFC(str) {
  const frequencyCounter = {};

  for (let char of str.replace(/[^\w]/g, '').toLowerCase()) {
    frequencyCounter[char] = frequencyCounter[char] + 1 || 1;
  }

  return frequencyCounter;
}
// Longest Word
// Return the longest word of a string
// If more than one longest word, put into an array
// ex. longestWord('Hello, my name is Brad') === 'hello'
// ex. longestWord('Hello there, my name is Brad') === ['hello', 'there']
/*
Implement A Function Called Count Unique Values which accepts a SORTED ARRAY
HINT HINT
and counts the unique values in the array.
There can be negative numbers in the array, but it will always be sorted!!!
// ex. countUniqueValues([1,2,3,4,5,6]) === 6
*/

function countUniqueValues(arr){
    if(arr.length === 0) return 0;
    let i = 0;
    for(let j = 1; j < arr.length; j++){
        if(arr[i] !== arr[j]){
            i++;
            arr[i] = arr[j]
        }
    }
    return i + 1;
}
/*
Given a string and a positive number k, find the longest substring of the string containing exactly k distinct characters. If k is more than the total number of distinct characters in the string, return the whole string.
// ex. longestSubstring('Arthur', 4) === 'rthur'
its 4 unique charcters and and 5 charcters in length 'Arthu' has 5 distcinct characters so it does not qualify, in `rthur` there are 4 distinct characters because r is repeated.
*/

function longestSubstring(str, k) {
    let maxLength = 0;
    let start = 0;
    let end = 0;
    let charCount = new Map();
    let distinctCount = 0;
    while (end < str.length) {
        if (!charCount.has(str[end])) {
            distinctCount++;
        }
        charCount.set(str[end], (charCount.get(str[end]) || 0) + 1);
        end++;
        while (distinctCount > k) {
            charCount.set(str[start], charCount.get(str[start]) - 1);
            if (charCount.get(str[start]) === 0) {
                distinctCount--;
            }
            start++;
        }
        maxLength = Math.max(maxLength, end - start);
    }
    return str.substring(start, start + maxLength);
}