-2

I am using visual c# to write a program of matching words from a word-list. I need to match the letters of a string with another string and if it satisfies then I will select those.

i.e.: 1st example: 1st string: "LANE" 2nd string: "CLEAN"

2nd example: 1st string: "AGED" 2nd string: "CAGED"

3rd example: 1st string: "AGED" 2nd string: "RAGE"

4th example: 1st string: "CANCEL" 2nd string: "CONCEAL"

For me, the 1st and 2nd example both should come out to be true. But the 3rd one should be false.

What I could do so far is that I can get the 2nd example return true with string.contains() method. Please help me get the 1st example true as well.

Many Thanks in advance.

I have added another example. the 4th one. Please help.

  • What about other edge cases? e.g. 1 = "RED", 2= "FREEDOM" 1 = "REED", 2 = "DREAM" What are your expected results for those cases? – ZombieSheep Jun 13 '14 at 11:09
  • possible duplicate of [String similarity algorithims?](http://stackoverflow.com/questions/3576211/string-similarity-algorithims) – Marko Gresak Jun 13 '14 at 11:11
  • do you need pattern matching, not only letters matching, right? – vvv Jun 13 '14 at 11:13
  • @ZombieSheep All 3 cases should return true. Thanks – user3737402 Jun 13 '14 at 11:18
  • @user3737402 You might want to add the examples into your question. There are some decent answers already but they will fail on these cases. – ZombieSheep Jun 13 '14 at 11:28
  • @ZombieSheep you are absolutely right. I am facing problems. I didnt notice before. can you suggest anything about how to solve this. – user3737402 Jun 13 '14 at 15:27

3 Answers3

1
using System;
using System.Linq;


public class Program {
    public static void Main() {

        Console.WriteLine("LANE/CLEAN: {0}", Test("LANE", "CLEAN"));
        Console.WriteLine("AGED/CAGED: {0}", Test("AGED", "CAGED"));
        Console.WriteLine("AGED/RAGE: {0}", Test("AGED", "RAGE"));
        Console.WriteLine("CANCEL/CONCEAL: {0}", Test("CANCEL", "CONCEAL"));

    }

    public static bool Test(string s1, string s2) { 

        var sourceLetters = s1.ToCharArray().ToList();

        foreach (var letter in s2.ToCharArray())
            if (sourceLetters.Contains(letter))
                sourceLetters.Remove(letter);

        return !sourceLetters.Any();        

    }
}

// Results:
//
// LANE/CLEAN: True
// AGED/CAGED: True
// AGED/RAGE: False
// CANCEL/CONCEAL: True
BG100
  • 4,481
  • 2
  • 37
  • 64
1
    public bool StringSimularity(string s1, string s2)
    {
       var res =  s1.Intersect(s2);
       if (res.Count().Equals(s2.Length) || res.Count().Equals(s1.Length))
           return true;
       else
           return false;
    }
MRebai
  • 5,344
  • 3
  • 33
  • 52
0

Something like this?

    string word = "LANE";

    string[] wordList = { "CLEAN", "AGED", "CAGED", "RAGE" };

    foreach (string w in wordList)
    {
        Boolean x = true;
        foreach (char c in word)
        {
            if (!w.Contains(c))
            {
                x = false;
                break;
            }
        }
        if (x)
            Console.WriteLine(w);
Jules
  • 567
  • 1
  • 6
  • 15