Game of Thrones - I

/*

 Dothraki are planning an attack to usurp King Robert's throne. King Robert learns of this conspiracy from Raven and
 plans to lock the single door through which the enemy can enter his kingdom.
door

But, to lock the door he needs a key that is an anagram of a certain palindrome string.
The king has a string composed of lowercase English letters. Help him figure out whether any anagram of the string
can be a palindrome or not.
Input Format
A single line which contains the input string.
Constraints
1<= length of string  <=10^5
Each character of the string is a lowercase English letter.
Output Format
A single line which contains YES or NO in uppercase.
Sample Input : 01
aaabbbb
Sample Output : 01
YES
Explanation
A palindrome permutation of the given string is bbaaabb.
Sample Input : 02
cdefghmnopqrstuvw
Sample Output : 02
NO
Explanation
You can verify that the given string has no palindrome permutation.
Sample Input : 03
cdcdcdcdeeeef
Sample Output : 03
YES
Explanation
A palindrome permutation of the given string is ddcceefeeccdd.
 

 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HackerDemo
{
    class GameofThronesI
    {
       static string resultStr = "NO";
        public void GameofThronesImain()
        {
            string inputstr = "cdefghmnopqrstuvw";// Console.ReadLine();
            char[] arr = inputstr.ToCharArray();
            GetPer(arr);
            Console.WriteLine(resultStr);
        }

        public static bool IsPalindrome(string str)
        {
            return str.SequenceEqual(str.Reverse());
        }
       private static void Swap(ref char a, ref char b)
        {
            if (a == b) return;
            a ^= b;
            b ^= a;
            a ^= b;
        }

        public static void GetPer(char[] list)
        {
            int x = list.Length - 1;
            GetPer(list, 0, x);
        }

        private static void GetPer(char[] list, int k, int m)
        {
            
            if (k == m)
            {
                if (IsPalindrome(new string(list))&& resultStr=="NO")
                {
                    resultStr = "YES";
                }
            }
            else
                for (int i = k; i <= m; i++)
                {
                    Swap(ref list[k], ref list[i]);
                    GetPer(list, k + 1, m);
                    Swap(ref list[k], ref list[i]);
                }
        }
    }
}

No comments:

Post a Comment