Advertisement
VladoG

[Programming Fundamentals] (Arrays)- 01. Largest Common End

Jun 6th, 2016
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 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 _01.Largest_Common_End
  8.     {
  9.     class LargestCommonEnd
  10.         {
  11.         static void Main(string[] args)
  12.             {
  13.             string[] words1 = Console.ReadLine().Split().ToArray();
  14.             string[] words2 = Console.ReadLine().Split().ToArray();
  15.             int size = Math.Min(words1.Length, words2.Length);
  16.             int length1 = words1.Length;
  17.             int length2 = words2.Length;
  18.             int countLeft = 0, countRight = 0;
  19.  
  20.             // CHECK LEFT side
  21.             for (int i = 0; i < size; i++)
  22.                 {
  23.                 if (words1[i]==words2[i])
  24.                     {
  25.                     countLeft++;
  26.                     }
  27.                 else
  28.                     {
  29.                     break;
  30.                     }
  31.                 }
  32.  
  33.             // CHECK RIGHT side
  34.             for (int i = 1; i < size; i++)
  35.                 {
  36.                 if (words1[length1-i]==words2[length2-i])
  37.                     {
  38.                     countRight++;
  39.                     }
  40.                 else
  41.                     {
  42.                     break;
  43.                     }
  44.                 }
  45.  
  46.             if (countLeft==0 && countRight==0)
  47.                 {
  48.                 Console.WriteLine(countLeft);
  49.                 }
  50.             else
  51.                 {
  52.                 Console.WriteLine(Math.Max(countLeft,countRight));
  53.                 }
  54.  
  55.  
  56.             } // End of Main
  57.         }
  58.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement