简体   繁体   中英

C# algorithm searching words by numbers

I have to write algotithm in C# where input is:

3 2
aj
oj
ck
25
73

and the output:

aj ck
DOES NOT EXIST

The first line in iput is two numbers, the first one is number of words. And the second one is number of numbers for which we will be searching for words. Each character is represented by number like this below:

 2 (abc)
 3 (def)
 4 (ghi)
 5 (jkl)
 6 (mno)
 7 (pqrs)
 8 (tuv)
 9 (wxyz)

For example for number 25, there are 9 two-char-words: aj, ak, al, bj, bk, bl, cj, ck, cl. For number 438, there are 27 three-char-words, etc.

So far I have this code, but something is not working properly, and I have no idea what is it that is not working.

int n;
int k;
string input;
List<string> dict = new List<string>();
List<string> res = new List<string>();
n = int.Parse(Console.ReadLine());
k = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
    input = Console.ReadLine();
    dict.Add(input);
}
dict.Sort();
for (int i = 0; i < k; i++)
{
    input = Console.ReadLine();
    res = new List<string>(dict);
    int inputLength = input.Length;
    for (int j = 0; j < inputLength; ++j)
    {
        switch (input[j])
        {
            case '2':
                res = new List<string>(filter(res, i, 'a', 'b', 'c'));
                break;
            case '3':
                res = new List<string>(filter(res, i, 'd', 'e', 'f'));
                break;
            case '4':
                break;
            case '5':
                res = new List<string>(filter(res, i, 'j', 'k', 'l'));
                break;
            case '6':
                res = new List<string>(filter(res, i, 'm', 'n', 'o'));
                break;
            case '7':
                res = new List<string>(filterWithFour(res, i, 'p', 'q', 'r', 's'));
                break;
            case '8':
                res = new List<string>(filter(res, i, 't', 'u', 'v'));
                break;
            case '9':
                res = new List<string>(filterWithFour(res, i, 'w', 'x', 'y', 'z'));
                break;
        }
    }
    if (res.Any())
        foreach (var item in res.ToList())
            Console.WriteLine(item);
    else
        Console.WriteLine("DOES NOT EXIST");
}

static List<string> filterWithFour(List<string> resGiven, int pos, char a, char b, char c, char d)
{
    List<string> res = new List<string>();
    foreach (var item in resGiven.ToList())
    {
        if (item.Length > pos)
            if (item[pos] == a || item[pos] == b || item[pos] == c || item[pos] == d)
                res.Add(item);
    }
    return res;
}

static List<string> filter(List<string> resGiven, int pos, char a, char b, char c)
{
    List<string> res = new List<string>();
    foreach (var item in resGiven.ToList())
    {
        if (item.Length > pos)
            if (item[pos] == a || item[pos] == b || item[pos] == c)
                res.Add(item);
    }
    return res;
}

Thank you for any help, because I stuck on this one, and I can't go through it.

In order to do what you want, I would replace the numbers with Regex pattern as such:

using System.IO;
using System;
using System.Collections.Generic;
using System.Linq; // so you can use .Where on List
using System.Text.RegularExpressions;

class Program
{
    static void Main()
    {
        int n;
        int k;
        string input;
        List<string> dict = new List<string>();
        n = int.Parse(Console.ReadLine());
        k = int.Parse(Console.ReadLine());
        for (int i = 0; i < n; i++)
        {
            input = Console.ReadLine();
            dict.Add(input);
        }
        dict.Sort();
        for (int i = 0; i < k; i++)
        {
            input = Console.ReadLine();

            // get the full pattern for the whole number
            string patternToSearch = "";
            foreach(var c in input)
            {
                patternToSearch += GetPattern(c);
            }

            Console.WriteLine(patternToSearch);

            // find the words that matches the pattern
            var filteredDict = dict.Where(w => Regex.Match(w, patternToSearch).Success);

            if(!filteredDict.Any())
                Console.WriteLine("DOES NOT EXIST");
            else
                Console.WriteLine(string.Join(" ", filteredDict));
        }
    }

    // returns the regex pattern for one number
    static string GetPattern(char c)
    {
        switch(c)
        {
            case '2': return "[abc]";
            case '3': return "[def]";
            case '4': return "[ghi]";
            case '5': return "[jkl]";
            case '6': return "[mno]";
            case '7': return "[pqrs]";
            case '8': return "[tuv]";
            case '9': return "[wxyz]";
            default: return "";
        }
    }
}

With input:

3
2
aj
oj
ck
25
73

It outputs:

[abc][jkl]
aj ck
[pqrs][def]
DOES NOT EXIST

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM