Advertisement
_CodeBehind

1.Largest_Common_End

Jan 23rd, 2017
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _1.Largest_Common_End
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] array1 = Console.ReadLine().Split(' ');
  14.             string[] array2 = Console.ReadLine().Split(' ');
  15.  
  16.             int largestArrayCount = array1.Length > array2.Length ? array1.Length : array2.Length;
  17.  
  18.             bool leftSideMatch = true;
  19.             bool rightSideMatch = true;
  20.             int leftCount = 0;
  21.             int rightCount = 0;
  22.             bool KeyExist = false;
  23.  
  24.             for (int i = 0; i < largestArrayCount; i++)
  25.             {
  26.                 KeyExist = array1.Length > i && array2.Length > i ? true : false;
  27.                 if (KeyExist && (!string.IsNullOrEmpty(array1[i]) || !string.IsNullOrEmpty(array2[i])) && leftSideMatch)
  28.                 {
  29.                     if (array1[i] == array2[i])
  30.                     {
  31.                         leftCount++;
  32.                     }
  33.                     else
  34.                     {
  35.                         leftSideMatch = false;
  36.                     }
  37.                 }
  38.                 KeyExist = array1.Length > array1.Length - 1 - i && array2.Length > array2.Length - 1 - i ? true : false;
  39.                 if (KeyExist && (!string.IsNullOrEmpty(array1[array1.Length - 1 - i]) || !string.IsNullOrEmpty(array2[array2.Length - 1 - i])) && rightSideMatch)
  40.                 {
  41.                     if (array1[array1.Length - 1 - i] == array2[array2.Length - 1 - i])
  42.                     {
  43.                         rightCount++;
  44.                     }
  45.                     else
  46.                     {
  47.                         rightSideMatch = false;
  48.                     }
  49.                 }
  50.                 if (rightSideMatch == false && leftSideMatch == false)
  51.                 {
  52.                     break;
  53.                 }
  54.             }
  55.             Console.WriteLine(leftCount > rightCount ? leftCount : rightCount);
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement