using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

public static class Karakterek
{
    public static List<char> KarakterekList = new List<char>();
    public static void ReadDataFromFile(string path)
    {
        KarakterekList = File.ReadAllText(path).ToCharArray().ToList();
    }

    public static List<char> LongestStringWithTwoCharactersOnly()
    {
        if (KarakterekList.Count < 2)
            return new List<char>();

        List<char> currentBestCharacterSequence = new List<char>();
        List<char> currentCharacterSequence = new List<char>();

        char firstChar = KarakterekList[0];
        char secondChar = KarakterekList[1];

        currentCharacterSequence.Add(firstChar);
        currentCharacterSequence.Add(secondChar);

        for (int i = 2; i < KarakterekList.Count; i++)
        {
            char item = KarakterekList[i];

            if (item == firstChar || item == secondChar)
            {
                currentCharacterSequence.Add(item);
            }
            else
            {
                if (currentCharacterSequence.Count > currentBestCharacterSequence.Count)
                {
                    currentBestCharacterSequence = new List<char>(currentCharacterSequence);
                }

                firstChar = currentCharacterSequence[currentCharacterSequence.Count - 1];
                secondChar = item;

                currentCharacterSequence.Clear();
                currentCharacterSequence.Add(firstChar);
                currentCharacterSequence.Add(secondChar);
            }
        }

        if (currentCharacterSequence.Count > currentBestCharacterSequence.Count)
        {
            currentBestCharacterSequence = currentCharacterSequence;
        }

        return currentBestCharacterSequence;
    }

    public static int CountABCSequences(string input)
    {
        int count = 0;
        for (int i = 0; i < input.Length; i++)
        {
            if (input[i] == 'a')
            {
                for (int j = i + 1; j < input.Length; j++)
                {
                    if (input[j] == 'b')
                    {
                        for (int k = j + 1; k < input.Length; k++)
                        {
                            if (input[k] == 'c')
                            {
                                count++;
                            }
                        }
                    }
                }
            }
        }
        return count;
    }

    public static int TotalDistanceTraveled(string input)
    {
        int x = 0;
        int y = 0;

        for (int i = 0; i < input.Length; i++)
        {
            if (input[i] == 'b')
            {
                x++;
            }
            else if (input[i] == 'd')
            {
                x--;
            }
            else if (input[i] == 'a')
            {
                y++;
            }
            else if (input[i] == 'c')
            {
                y--;
            }
        }
        return (int)Math.Sqrt(Math.Pow(x, 2) + Math.Pow(y, 2));
    }
}

public static class Szoveg
{
    public static List<string> fullTextInListOfString = new List<string>();
    public static List<string> wordsWithUniqeCharactersOnly = new List<string>();


    public static void ReadDataFromFile(string path)
    {
        var lines = File.ReadAllText(path).Split(new string[] { Environment.NewLine }, StringSplitOptions.None).ToList();

        foreach (var line in lines)
        {
            var allStringsInLine = line.Split(' ');
            foreach (var str in allStringsInLine)
            {
                fullTextInListOfString.Add(str);
            }
        }
    }

    public static List<string> WordsWithUniqeCharactersOnly()
    {
        foreach (var word in fullTextInListOfString)
        {
            if (word.Distinct().Count() == word.Length)
            {
                wordsWithUniqeCharactersOnly.Add(word);
            }
        }

        return wordsWithUniqeCharactersOnly;
    }

    public static string LongestStringWithUniqueCharacters()
    {
        return wordsWithUniqeCharactersOnly.OrderByDescending(s => s.Length).First();
    }

    public static int GreatestDistanceBetweenWords(string input)
    {
        int maxDistance = 0;

        int firstEndIndex = 0;

        int secondStartIndex = 0;
        int secondEndIndex = 0;

        for (int i = 1; i < input.Length - 2; i++)
        {
            // új névelő megtalálva
            if (input[i] == 'A' && input[i - 1] == ' ' && input[i + 1] == ' ')
            {
                if (firstEndIndex == 0)
                {
                    firstEndIndex = i;
                    continue;
                }

                if (secondEndIndex == 0 && firstEndIndex != 0)
                {
                    secondStartIndex = i;
                    secondEndIndex = i;
                }

                // kettő közötti távolság
                int distance = Convert.ToInt32(secondStartIndex - firstEndIndex - 1);
                if (distance > maxDistance)
                {
                    maxDistance = distance;
                }


                firstEndIndex = secondEndIndex;

                secondStartIndex = i;
                secondEndIndex = i;

                continue;
            }

            if (input[i] == 'A' && input[i + 1] == 'Z' && input[i - 1] == ' ' && input[i + 2] == ' ')
            {
                if (firstEndIndex == 0)
                {
                    firstEndIndex = i;
                    continue;
                }

                if (secondEndIndex == 0 && firstEndIndex != 0)
                {
                    secondStartIndex = i;
                    secondEndIndex = i + 1;
                }

                // kettő közötti távolság
                int distance = Convert.ToInt32(secondStartIndex - firstEndIndex - 1);
                if (distance > maxDistance)
                {
                    maxDistance = distance;
                }

                firstEndIndex = secondEndIndex;

                secondStartIndex = i;
                secondEndIndex = i + 1;
            }
        }

        return maxDistance;
    }

    public static int MaxDistanceBetweenArticles(string text)
    {
        List<int> distances = new List<int>();
        Regex regex = new Regex(@"\b(AZ|A)\b", RegexOptions.IgnoreCase);

        foreach (Match match in regex.Matches(text))
        {
            int startPosition = match.Index;
            int endPosition = match.Index + match.Length - 1; 
            
            var asd = match.NextMatch();
            var nextStart = asd.Index;
            var nextEnd = asd.Index + asd.Length - 1;

            distances.Add(nextStart - endPosition - 1);
        }


        return distances.Max(); // Max distance
    }

    public static int UniquePalindromes()
    {
        List<string> palindromes = new List<string>();

        foreach (var word in fullTextInListOfString)
        {
            if  (word.Length < 2)
            {
                continue;
            }

            if (word == new string(word.Reverse().ToArray()))
            {
                if (!palindromes.Contains(word))
                {
                    palindromes.Add(word);
                }
            }
        }
        return palindromes.Count;
    }
}

public class Program
{
    public static void Main(string[] args)
    {
        Karakterek.ReadDataFromFile(@"..\karsor.txt");

        //System.Console.WriteLine($"{Karakterek.KarakterekList.Count}");

        // feladat 1 a
        /*
        var longestStringWithTwoCharactersOnly = Karakterek.LongestStringWithTwoCharactersOnly();
        var karakterek = longestStringWithTwoCharactersOnly.Distinct().OrderBy(c => c).ToArray();
        int longestStringWithTwoCharactersOnlyLength = longestStringWithTwoCharactersOnly.Count;

        System.Console.WriteLine($"{karakterek[0]}{karakterek[1]}{longestStringWithTwoCharactersOnlyLength}");
        */

        // feladat 1 b

        // ez az input változó volt használva a b és a c részben is
        //string input = new string(Karakterek.KarakterekList.ToArray());

        //System.Console.WriteLine($"{Karakterek.CountABCSequences(input)}");

        // feladat 1 c
        //System.Console.WriteLine($"{Karakterek.TotalDistanceTraveled(input)}");

        // feladat 2 a

        Szoveg.ReadDataFromFile(@"..\szoveg.txt");

        /*
        Szoveg.wordsWithUniqeCharactersOnly = Szoveg.WordsWithUniqeCharactersOnly();

        System.Console.WriteLine($"{Szoveg.LongestStringWithUniqueCharacters()}");
        */

        // feladat 2 b
        string fullTextInSingleString = string.Join(" ", Szoveg.fullTextInListOfString);

        //int value = Szoveg.GreatestDistanceBetweenWords(fullTextInSingleString);

        int value = Szoveg.MaxDistanceBetweenArticles(fullTextInSingleString);

        System.Console.WriteLine($"{value}");

        System.Console.WriteLine($"{Szoveg.UniquePalindromes()}");

    }
}