Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

c# LCP

 public static string LCP(string[] words)
        {
            string result = "";

            if (words.Length == 0 || words == null) return "None in common";
            // shortest to longest by word count ... optional
            var orderedWords = words.OrderBy(x => x.Length)
            				  .ToArray();

            for (int i = 0; i < words.Length - 1; i++)
            {
                //hold each word to compare against 1st word
                string fixedWord = words[0].Substring(0, i + 1);

                for (int j = 1; j < words.Length; j++)
                {
                    //compare 1st word by letter against each other word
                    string currentWord = words[j].Substring(0, i + 1);

                    if (fixedWord == currentWord)
                    {
                        // just the char's that match
                        result = currentWord;
                    }
                    else if (fixedWord != currentWord)
                    {
                        return result.Substring(0, currentWord.Length - 1);
                    }
                }
            }
            return result;

        }
Comment

PREVIOUS NEXT
Code Example
Csharp :: c# loop through files in folder 
Csharp :: get query string parameter from string value c# 
Csharp :: c# countdown timer menutes 
Csharp :: datagridview column color c# 
Csharp :: displayname c# 
Csharp :: casting string to enum type 
Csharp :: C# How to write Hello World 
Csharp :: how to make error sound c# 
Csharp :: json.net deserialize dynamic 
Csharp :: shuffle arraylist c# 
Csharp :: c# find duplicates in list of strings 
Csharp :: unity normalize float 
Csharp :: how to make an object appear and disappear in unity 
Csharp :: c# messagebox yes no "wpf" 
Csharp :: enum get all values c# 
Csharp :: c# map 
Csharp :: how to print dictionary in c# 
Csharp :: c# append text to file 
Csharp :: how to set the frame rate unity 
Csharp :: c# remove double quotes from string 
Csharp :: asp.net core 3.1 ajax partial view 
Csharp :: convert object to array in c# 
Csharp :: c# datagridview cell click event 
Csharp :: system.io.directorynotfoundexception c# 
Csharp :: total months between two dates c# 
Csharp :: how to add to a list c# 
Csharp :: c# convert stream to memorystream 
Csharp :: 2 rotation unity 
Csharp :: get value from config file c# 
Csharp :: unity random point in sphere 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =