Oh I know already, my prefix and suffix checking is wrong.
It should be using StartsWith and EndsWith instead of Contains

// Validate prefix
            for (int i = 0; i < numString && valid; i++)
            {
                // all prefix must be substring of the longest prefix
                if (token[i][0] != "*" && !prefix.StartsWith(token[i][0]))
                    valid = false;
            }

            // Validate suffix
            for (int i = 0; i < numString && valid; i++)
            {
                // all suffix must be substring of the longest prefix
                if (token[i][token[i].Length - 1] != "*" && 
!suffix.EndsWith(token[i][token[i].Length - 1]))
                    valid = false;
            }



On Tuesday, 14 April 2020 00:11:13 UTC+8, RC's wrote:
>
> Hi, I'm trying to solve this problem, and based on the analysis provided, 
> I came out with below code:
>
> using System;
> using System.Collections.Generic;
> using System.Linq;
> using System.Text;
> using System.Threading.Tasks;
> using System.IO;
> using System.Text.RegularExpressions;
>
> namespace MyProgram
> {
>     class Solver
>     {
>         public void Run()
>         {
>             int cases, numCase;
>
>             //Console.SetIn(File.OpenText("input.txt"));
>             //StreamWriter sw = new StreamWriter("output.txt");
>             //Console.SetOut(sw);
>
>             string input;
>
>             input = Console.ReadLine();
>             cases = Int32.Parse(input);
>
>             for (numCase = 1; numCase <= cases; numCase++)
>                 Solve(numCase);
>
>             //sw.Close();
>         }
>
>         public void Solve(int numCase)
>         {
>             StringBuilder sb = new StringBuilder();
>             string[] data;
>             string[][] token;
>             string prefix = string.Empty, suffix = string.Empty;
>             string answer = string.Empty;
>
>             string input = Console.ReadLine();
>             int numString = Convert.ToInt32(input);
>             data = new string[numString];
>             token = new string[numString][];
>             for (int i = 0; i < numString; i++)
>             {
>                 data[i] = Console.ReadLine();
>                 token[i] = Regex.Split(data[i], @"(\*)").Where(x => 
> !string.IsNullOrEmpty(x)).ToArray();
>             }
>             
>             // Get the longest prefix
>             for(int i = 0; i < numString; i++)
>             {
>                 if (token[i][0] != "*" && token[i][0].Length > 
> prefix.Length)
>                     prefix = token[i][0];
>             }
>
>             // Get the longest suffix
>             for (int i = 0; i < numString; i++)
>             {
>                 if (token[i][token[i].Length - 1] != "*" && 
> token[i][token[i].Length - 1].Length > suffix.Length)
>                     suffix = token[i][token[i].Length - 1];
>             }
>
>             bool valid = true;
>
>             // Validate prefix
>             for (int i = 0; i < numString && valid; i++)
>             {
>                 // all prefix must be substring of the longest prefix
>                 if (token[i][0] != "*" && !prefix.Contains(token[i][0]))
>                     valid = false;
>             }
>
>             // Validate suffix
>             for (int i = 0; i < numString && valid; i++)
>             {
>                 // all suffix must be substring of the longest prefix
>                 if (token[i][token[i].Length - 1] != "*" && 
> !suffix.Contains(token[i][token[i].Length - 1]))
>                     valid = false;
>             }
>
>             if (!valid)
>                 Console.WriteLine("Case #{0}: {1}", numCase, "*");
>             else
>             {
>                 // Build the string
>                 sb.Append(prefix);
>                 for (int i = 0; i < numString; i++)
>                 {
>                     // No need first or last token since prefix and suffix 
> is already in the answer
>                     for (int j = 1; j < token[i].Length - 1; j++)
>                     {
>                         if (token[i][j] != "*")
>                         {                            
>                             sb.Append(token[i][j]);
>                         }
>                     }
>                 }
>
>                 sb.Append(suffix);
>                 
>                 answer = sb.ToString();
>
>                 Console.WriteLine("Case #{0}: {1}", numCase, answer);
>             }
>         }
>     }
>
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             Solver solver = new Solver();
>             solver.Run();
>         }
>     }
> }
>
>
> But I'm getting Wrong Answer.
> Anyone knows where my fault is ? Or can anyone provide test case where it 
> fails?
>
> Thank you.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google Code Jam" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/google-code/27292fd0-b293-4603-bfd9-57368f8e26ea%40googlegroups.com.

Reply via email to