Skip to content

Instantly share code, notes, and snippets.

@naonao0001777
Last active December 19, 2020 16:54
Show Gist options
  • Save naonao0001777/61a0384fb664ed782be0e8673fa2a1d1 to your computer and use it in GitHub Desktop.
Save naonao0001777/61a0384fb664ed782be0e8673fa2a1d1 to your computer and use it in GitHub Desktop.
世界で闘うプログラミング力を鍛える本-コーディング面接189問とその開放-の解

重複を調べる

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

namespace CheckOverrapString
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();

            string answer = "文字は重複していません。";

            List<char> strList = new List<char>();

            for (int i = 0;i<str.Length;i++)
            {
                if (strList.Contains(str[i]))
                {
                    answer = "文字が重複しています。";
                    break;
                }
                strList.Add(str[i]);

            }
            Console.Write(answer);
            Console.ReadKey();
        }
    }
}

文字をコンバートするなんか

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

namespace OneTimeConversion
{
    class Program
    {
        static void Main(string[] args)
        {
            string first = Console.ReadLine();
            string second = Console.ReadLine();

            string largeString = string.Empty;
            string smallString = string.Empty;

            bool sameLength = false;

            if (first.Length > second.Length)
            {
                largeString = first;
                smallString = second;
            }
            else if (first.Length < second.Length)
            {
                largeString = second;
                smallString = first;
            }
            else
            {
                sameLength = true;
                largeString = first;
                smallString = second;
            }

            List<char> charList = new List<char>();
            string answer = "True";

            if (sameLength)
            {
                for (int i = 0; i < largeString.Length; i++)
                {
                    if (largeString[i] != smallString[i])
                    {
                        smallString =  smallString.Replace(smallString[i], largeString[i]);

                        if (smallString != largeString)
                        {
                            answer = "False";
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                for (int i = 0; i < largeString.Length; i++)
                {
                    if (largeString[i] != smallString[i])
                    {
                        smallString = smallString.Insert(i, largeString[i].ToString());

                        if (smallString != largeString)
                        {
                            answer = "False";
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
            }
            Console.Write(answer);
            Console.ReadKey(true);
        }
    }
}

回文する文字を作成する

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

namespace PalindromicPermutation
{
    class Program
    {
        static void Main(string[] args)
        {
            string words = Console.ReadLine();

            List<char> chrList = new List<char>();

            List<char> searchList = new List<char>();

            string answer = "True";
            
            int countWords = 0;
            
            foreach (var word in words)
            {
                // 空白文字を無視して結合する
                if (word != ' ' || !char.IsWhiteSpace(word))
                {
                    if (!chrList.Contains(word))
                    {
                        // 文字列全部格納リスト内検索用文字列リスト
                        searchList.Add(word);
                        
                        // 文字列全部格納リスト
                        chrList.Add(word);
                    }
                    else
                    {
                        // 文字列全部格納リスト
                        chrList.Add(word);
                    }
                }
            }

            foreach (var sw in searchList)
            {
                // 検索用文字列リスト内の文字が文字列全部格納リストに含まれている数
                int count = chrList.Where(a => a == sw).Count();

                // 奇数個の場合
                if (count%2 == 1)
                {
                    countWords++;

                    // 奇数個文字が複数ある場合
                    if (countWords > 1)
                    {
                        answer = "False";
                        break;
                    }
                }
                else
                {
                    continue;
                }
            }

            Console.Write(answer);
            Console.ReadKey(true);

        }
    }
}

文字列圧縮

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

namespace StringCompression
{
    class Program
    {
        static void Main(string[] args)
        {
            string inputString = Console.ReadLine();
            string outputString = string.Empty;
            int count = 1;

            for(int i = 0;i<inputString.Length;i++)
            {
                if (i + 1 >= inputString.Length||inputString[i]!=inputString[i+1])
                {
                    outputString += string.Concat(inputString[i],count);
                    count = 1;
                }
                else
                {
                    count++;
                }
            }

            // tureの場合は左、falseの場合は右を評価する
            outputString = outputString.Length >= inputString.Length ? inputString : outputString;
           
            Console.Write(outputString);
            Console.ReadKey(true);
        }
    }
}

URLify

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

namespace URLify
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();
            string [] sepStr = str.Split(',');

            int lengthStr = 0;

            string words = string.Empty;
            int count = 0;
            foreach (var len in sepStr)
            {
                if (count == 0)
                {
                    words = len;
                    count++;
                }
                else
                {
                    lengthStr = int.Parse(len);
                }
            }

            string outputWords = string.Empty;

            for (int i = 0; i < lengthStr;i++)
            {
                if (words[i] == ' ')
                {
                    outputWords += "%20";
                    continue;
                }

                outputWords+=words[i];
            }

            Console.Write(outputWords);
            Console.ReadKey();
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment