Advertisement
koksibg

Largest_Common_End

Oct 5th, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 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 Largest_Common_End
  8. {
  9.     class Largest_Common_End
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string[] firstArray = Console.ReadLine().Split(' ').ToArray();
  14.             string[] secondArray = Console.ReadLine().Split(' ').ToArray();
  15.             int output = LargestCommonEnd(firstArray, secondArray);
  16.             Console.WriteLine(output);
  17.         }
  18.  
  19.         static int LargestCommonEnd(string[] firstArray, string[] secondArray)
  20.         {
  21.             var leftCount = 0;
  22.             var rightCount = 0;
  23.             while ((leftCount < firstArray.Length && leftCount < secondArray.Length) &&
  24.                    (rightCount < firstArray.Length && rightCount < secondArray.Length))
  25.             {
  26.                 if (firstArray[leftCount] == secondArray[leftCount]) leftCount++;
  27.                 else if (firstArray[firstArray.Length - rightCount - 1] == secondArray[secondArray.Length - rightCount - 1]) rightCount++;
  28.                 else break;
  29.             }
  30.            
  31.             if (leftCount < rightCount) return rightCount;
  32.             else return leftCount;
  33.         }
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement