Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

I have a list of strings. I have to match a character array in the list in such a way that the strings in list must contain only characters in the array.

For ex: if my character array (_currentLetters string in the code below) is abdfuwz. Then the possible list of selected strings from the list would be bad, dab etc.

Below is one possible solution I have:

C#
public string[] GetPossibleWords()
{
    wordsList.Clear();

    IEnumerable<string> words = from w in _list.WordListCollection
                                where (w.Length > 1 &&
                                        w.IndexOfAny(_currentLetters.ToArray()) >= 0 &&
                                        w.Length <= _currentLetters.Length)
                                select w;

    // Check the sorted array of current letters. If the string
    // is a substring of this, then the word is in
    foreach (string s in words)
    {
        char[] toMatch = s.ToCharArray();
        Array.Sort(toMatch);
        string m = new string(toMatch);
        if (_currentLetters.Contains(m))
        {
            wordsList.Add(s);
        }
    }

    return wordsList.ToArray();
}


As you can see, I am searching words in my list of words. Once I get the word list, I am iterating again with all the words we found and making sure that the found word is a substring of the character array. This character array is a sorted array of characters. I am not sure if this is the best way to achieve my goal. Is there a better way? Can I write the entire code using Linq?

Thanks
Posted

1 solution

Hi,

Its better to use RegEx as like the below one,

C#
class Program
    {
        static void Main(string[] args)
        {

            List<string> listOfPossibleStrings = GetStringsWithPossibleChars("abdfuwz", 
                new List<string>() 
                { 
                    "jagan", 
                    "bad", 
                    "dad", 
                    "well", 
                    "test"
                });

        }

        private static List<string> GetStringsWithPossibleChars(string pattern,List<string> lstStrings)
        {
            var newList = from str in lstStrings 
                          where (new Regex(@"^[\s\d" + pattern + "]*$", RegexOptions.Singleline).IsMatch(str)== true) 
                          select str;

            return (List<string>)newList;
        }

    }
 
Share this answer
 
v2
Comments
Vivek Pandey (V) 23-Feb-12 12:35pm    
Almost but not exactly. For ex: if my pattern is "adlu" then "dual" and "laud" are the possible matches but not "add" or "dad" as there is only one 'a' and 'd' in the list. I already tried with the Regex but couldn't get the result I was looking for.
Jaganathan Bantheswaran 24-Feb-12 0:45am    
Each char should come only once in the string ?
Vivek Pandey (V) 24-Feb-12 15:26pm    
Hi Jaganathan,

Yes each character should come only once in a string. So if my pattern is addfuwz then "add" is a possible word otherwise it's not.

Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900