Find longest word from dictionary based on offered letters c#

LeaV 81 Reputation points
2024-01-28T19:32:19.52+00:00

Hello everyone, please and need your help. I have three text boxes on the windows form:

  • In tetxBox1 there is a dictionary with many words (the longest words have 12 letters)
  • There are always 12 letters offered in textBox2
  • In textBox3, I want the result to be longest word from textBox1 composed of offered letters from textBox2
  • For example:
  • In textbox1.Text I have the following words:
  • characters
  • character
  • charity
  • act
  • acter
  • acted
  • successfully
  • wind
  • succession
  • windy
  • successful
  • window
  • windows
  • success
  • excellent
  • excel
  • excellence
  • In textBox2.Text I have following 12 letters offered:
  • euclsfcysuls
  • In textBox3.Text I want as result longest word from textBox1.Text than letters from textBox2.Text:
  • successfully
  • I am asking for your help if it is possible, some solution...
  • I hope you understood me, I apologize for my English if you didn't understand.
  • Thank you all in advance.Capture
Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,829 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,252 questions
{count} votes

Accepted answer
  1. WayneAKing 4,921 Reputation points
    2024-01-29T01:00:26.9866667+00:00

    This looks like a course assignment. If so, then you should write your own code solution. We don't do homework in these forums. We can offer suggestions to help move you in the right direction while you write your program. It isn't entirely clear (to me) what you are looking for when you say you want the longest line from the dictionary. It also isn't clear why textBox2 would always have 12 characters. Assuming you want to find a match for the scrambled word in texBox2, you can try code such as this in your button click event.

    char[] ca2 = textBox2.Text.ToCharArray();
    Array.Sort(ca2);            
    string s2 = new string(ca2);
    foreach (string s in textBox1.Lines)
    {
        if (s.Length != textBox2.Text.Length) continue;
        char[] ca1 = s.ToCharArray();
        Array.Sort(ca1);
        string s1 = new string(ca1);
        if (s1 == s2)
        {
            textBox3.Text = s;
            break;
        }
    }
    
    

1 additional answer

Sort by: Most helpful
  1. P a u l 10,406 Reputation points
    2024-01-28T23:52:10.1566667+00:00

    This might be what you're after. The assumptions that I've made are that not all characters from textBox2 need to match a word from textBox1, just the word with the most overlapping characters. Also I've interpreted "longest word" to mean the word with the most amount of overlapping characters, rather than the actual longest word (e.g. just because a long word matches one character wouldn't mean it's the longest matching word if a 2-letter word matched two characters. Is this what you were asking for?

    string textbox1 = @"
    characters
    character
    charity
    act
    acter
    acted
    successfully
    wind
    succession
    windy
    successful
    window
    windows
    success
    excellent
    excel
    excellence
    ";
    
    string[] dictionary = textbox1.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
    
    string textbox2 = "euclsfcysuls";
    
    var charsByOccurrence1 = textbox2.GroupBy(c => c, (k, v) => new { Char = k, Occurrences = v.Count() });
    
    (string Term, int Matched)? longestMatch = null;
    
    foreach (string item in dictionary) {
    	var charsByOccurrence2 = item.GroupBy(c => c, (k, v) => new { Char = k, Occurrences = v.Count() });
    
    	var charsMatched = charsByOccurrence1.Join(charsByOccurrence2, _ => _.Char, _ => _.Char, 
    		(a, b) => Math.Min(a.Occurrences, b.Occurrences)
    	)
    	.Sum();
    
    	if (longestMatch is null || charsMatched > longestMatch.Value.Matched) {
    		longestMatch = (item, charsMatched);
    	}
    }
    
    if (longestMatch is null) {
    	Console.WriteLine("No match");
    } else {
    	Console.WriteLine($"Longest matched: {longestMatch.Value.Term} ({longestMatch.Value.Matched} char(s))");
    }