Skip to content
Snippets Groups Projects

Find the Difference

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Kiryuu Sakuya
    Program.cs 1.48 KiB
    using System;
    using System.Collections.Generic;
    
    namespace leetcodecsharp
    {
        class Program
        {
    
            private static char FindTheDifference(string s, string t)
            {
                // turn string into int then do minus.
                int s_arr_sum = 0;
                char[] s_arr = s.ToCharArray();
                for (int i = 0; i < s.Length; i++)
                {
                    s_arr_sum += char.ToUpper(s_arr[i]);
                }
                int t_arr_sum = 0;
                char[] t_arr = t.ToCharArray();
                for (int i = 0; i < t.Length; i++)
                {
                    t_arr_sum += char.ToUpper(t_arr[i]);
                }
                return char.ToLower((char)(t_arr_sum - s_arr_sum));
            }
    
            //public static string GenerateRandom(int total)
            //{
            //    var chars = "abcdefghijklmnopqrstuvwxyz";
            //    var stringChars = new char[total];
            //    var random = new Random();
    
            //    for (int i = 0; i < stringChars.Length; i++)
            //    {
            //        stringChars[i] = chars[random.Next(chars.Length)];
            //    }
    
            //    var finalString = new String(stringChars);
            //    return finalString;
            //}
            public static void Main(string[] args)
            {
                string s = "abcde";
                // String t is generated by random shuffling string s
                // and then add one more letter at a random position.
                string t = "bgaedc";
                Console.WriteLine(FindTheDifference(s, t));
            }
        }
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment