Enjoy Cypher Techniques with String

Query: Design code for Cypher string that returns string with kth character present in the wheel in anti clock wise direction consist of capital letter alphabets only.




Constraints
0< k & k<10^5

                                                                Test Case Examples




First try it yourself than if you find any challenge take the help from solution

I have try to write a code that can help user to understand the best possible use of Collection, LINQ and String Builder class. There could be many technique to write the code for this problem, I have tried my technique you can try yours but keep in mind the complexity should be always the key before writing the solution.

Solution in C#


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

namespace Cypher
{
    class Program
    {
        public static void Main(string[] args)
        {
            string encrypted = Console.ReadLine();

            int k = Convert.ToInt32(Console.ReadLine().Trim());

            string result = simpleCipher(encrypted, k);

            Console.WriteLine(result);
            Console.Read();
        }

        public static string simpleCipher(string encrypted, int k)
        {
            encrypted = encrypted != null ? encrypted.ToUpper() : string.Empty;
            char firstChar = 'A';
            char lastChar = 'Z';
            StringBuilder build = new StringBuilder();
            if (k >= 1 && k <= Math.Pow(10, 5))
            {
                Dictionary asciiDic = new Dictionary();
                List lst = new List();
                for (char c = firstChar; c <= lastChar; c++)
                {
                    lst.Add(c.ToString());
                    if (!asciiDic.ContainsKey((int)c))
                    {
                        asciiDic.Add((int)c, c.ToString());
                    }
                }

                foreach (char c in encrypted)
                {
                    int key = asciiDic.FirstOrDefault(x => x.Value == c.ToString()).Key;
                    int temp = key - k;
                    if (temp < ((int)firstChar))
                    {
                        int diff = k > (lst.IndexOf(c.ToString()) + 1) ? k - (lst.IndexOf(c.ToString()) + 1) : 0;
                        if (diff >= 26) diff %= 26; 
                        temp = ((int)lastChar - diff);
                    }
                    string _value = "";
                    if (asciiDic.ContainsKey(temp))
                        _value = asciiDic[temp]; 
                    build.Append(_value);
                }
            }
            return build.ToString();
        }
    }
 }

Comments

Popular posts from this blog

Enjoy Number Technique with Tom and Jerry Fight Game