Reputation: 21
Find whether the input string can form a valid English word. Ideally, we need to compare the input string in the English dictionary. Though an API dictionary is not required to search for the word, I have taken a hardcoded string array(which acts as an English dictionary here). Below is the code. Could you please help me with what's wrong with the code? The output of the program should be the string of the dictionary if it matches the input string.
public class Program
{
public static string Jumble(string input)
{
string[] array = { "abstract", "car", "flight" };
Dictionary<char, int> inputDict = new Dictionary<char, int>();
for (int i = 0; i < input.Length; i++)
{
if (inputDict.ContainsKey(input[i]))
{
inputDict[input[i]]++;
}
else
{
inputDict.Add(input[i], 1);
}
}
Dictionary<char, int> arrayDict = new Dictionary<char, int>();
for (int j = 0; j < array.Length; j++)
{
for (int k = 0; k < array[j].Length; k++)
{
if (arrayDict.ContainsKey(array[j][k]))
{
arrayDict[array[j][k]]++;
}
else
{
arrayDict.Add(array[j][k], 1);
}
}
}
for (int j = 0; j < array.Length; j++)
{
for (int k = 0; k < array[j].Length; k++)
{
if(inputDict.ContainsKey(array[j][k]) && inputDict.ContainsValue(arrayDict[array[j][k]]) && array[j].Length == input.Length)
{
Console.WriteLine(array[j]);
}
else
{
Console.WriteLine("Doesn't match");
}
}
}
return "";
}
static void Main(string[] args)
{
string input = "tracabst";
Console.WriteLine(Jumble(input));
Console.ReadLine();
}
} ```
Upvotes: 0
Views: 229
Reputation: 21
public class Program
{
// Jumble: "tracabst";
// Dictionary: "abstract", "car", "flight"
public static string Jumble(string input)
{
string[] array = { "abstract", "car", "flight" };
string result = "";
List<char> listOfChar = new List<char>(input);
for (int j = 0; j < array.Length; j++)
{
for (int k = 0; k < array[j].Length; k++)
{
if (listOfChar.Contains(array[j][k]) && array[j].Length == input.Length)
{
listOfChar.Remove(array[j][k]);
result = array[j];
}
}
}
if(listOfChar.Count > 0)
{
return "Doesn't Match";
}
return result;
}
static void Main(string[] args)
{
string input = "tracabst";
Console.WriteLine(Jumble(input));
Console.ReadLine();
}
}
Upvotes: 1