Turing (Odd Words)

Odd Words Given two strings set1 and set2. find all Odd Words. A word is called Odd when it appears only once in one of the sets, and does not appear in the other set. Example 1: Input: set1: “turing community is the best”, set2=”turing community is the greatest” Output: [“best”,”greatest”] Example 2: Input: set1:”turing turing”, set2=”google” Output: [“google”] Constratins: 1 <= set1.lenght , set2.length<=200 set1 and set2 contains of lowercase letters and spaces set1 and set2 have no leading or trailling spaces set and set contains words separated by a single space using C#

using System;
using System.Collections.Generic;

class Program
{
static void Main(string[] args)
{
string set1 = “turing community is the best”;
string set2 = “turing community is the greatest”;

    List<string> oddWords = FindOddWords(set1, set2);

    foreach (string word in oddWords)
    {
        Console.WriteLine(word);
    }
}

static List<string> FindOddWords(string set1, string set2)
{
    List<string> oddWords = new List<string>();

    // Split the sets into words
    string[] set1Words = set1.Split(' ');
    string[] set2Words = set2.Split(' ');

    // Create a dictionary to keep track of word counts in each set
    Dictionary<string, int> set1WordCounts = new Dictionary<string, int>();
    Dictionary<string, int> set2WordCounts = new Dictionary<string, int>();

    // Count the words in set1
    foreach (string word in set1Words)
    {
        if (set1WordCounts.ContainsKey(word))
        {
            set1WordCounts[word]++;
        }
        else
        {
            set1WordCounts[word] = 1;
        }
    }

    // Count the words in set2
    foreach (string word in set2Words)
    {
        if (set2WordCounts.ContainsKey(word))
        {
            set2WordCounts[word]++;
        }
        else
        {
            set2WordCounts[word] = 1;
        }
    }

    // Find the odd words
    foreach (string word in set1WordCounts.Keys)
    {
        if (!set2WordCounts.ContainsKey(word) && set1WordCounts[word] == 1)
        {
            oddWords.Add(word);
        }
    }

    foreach (string word in set2WordCounts.Keys)
    {
        if (!set1WordCounts.ContainsKey(word) && set2WordCounts[word] == 1)
        {
            oddWords.Add(word);
        }
    }

    return oddWords;
}

}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *