如何编写函数以任何顺序返回给定单词(包括单词本身)的所有字谜。 [英] How can I write function returns all anagrams of a given word (including the word itself) in any order.

查看:55
本文介绍了如何编写函数以任何顺序返回给定单词(包括单词本身)的所有字谜。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

An anagram is a word formed from another by rearranging its letters, using all the original letters exactly once; for example, orchestra can be rearranged into carthorse.

Write a function which returns all anagrams of a given word (including the word itself) in any order.

For example GetAllAnagrams("abba") should return collection containing "aabb", "abab", "abba", "baab", "baba", "bbaa".

using System;

using System.Text;

using System.Collections.Generic;

public class AllAnagrams

{

public static ICollection<string> GetAllAnagrams(string str)
{

throw new NotImplementedException("Waiting to be implemented.");

}

public static void Main(string[] args)

{

ICollection<string> anagrams = GetAllAnagrams("abba"); foreach (string a in anagrams)

Console.WriteLine(a);

}
}





我的尝试:





What I have tried:

using System;
using System.Collections.Generic;

public class AllAnagrams
{
	public static ICollection<string> GetAllAnagrams(string str)
	{
		List<string> _List = new List<string>();
		bool[] noswitch = new bool[str.Length];
		getOptions(str, ref noswitch, ref _List, str.Length);
		ICollection<string> list = _List;
		return list;
	}

	public void getOptions(string word, ref bool[] noswitch, ref List<string> options, int size)
	{
		if (size > 2)
		{
			for (int i = 0; i < noswitch.Length; i++)
			{
				noswitch[i] = true;
				getOptions(word, ref noswitch, size - 1);
				noswitch[i] = false;
			}
		}
		else
		{
			for (int y = 0; y < noswitch.Length; y++)
			{
				int prevCount = -1;
				if (noswitch[y] == false)
				{
					if (prevCount == -1)
					{
						prevCount = y;
					}
					else
					{
						char[] array = word.ToCharArray();
						char charb = word[y];
						array[y] = array[prevCount];
						array[prevCount] = charb;
						string newWord = new string (array);
						if (!options.Contains(newWord))
						{
							options.Add(newWord);
							Console.WriteLine("added");
						}
					}
				}
			}
		}
	}

	public static void Main(string[] args)
	{
		ICollection<string> anagrams = GetAllAnagrams("abba");
		foreach (string a in anagrams)
			Console.WriteLine(a);
	}
}

推荐答案

引用:

我如何编写函数以任何顺序返回给定单词的所有字谜(包括单词本身)。

How can I write function returns all anagrams of a given word (including the word itself) in any order.



作为程序员,你的工作是创建算法解决特定问题,你不能依赖别人永远为你做,所以有一段时间你必须学习如何。越快越好。



拿一张纸和一支铅笔。做一些小样本A,AB,ABC,ABCD ...

首先,不要重复相同单词中的字母。

想想如何用机械方式描述你可以找到所有的字谜。

一旦你有一个有效的算法,精炼处理重复的字母。



- 学习一个或多个分析方法, EW Djikstra自上而下的方法是一个良好的开端。

https:// en.wikipedia.org/wiki/Top-down_and_bottom-up_design [ ^ ]

https://en.wikipedia.org/wiki/Structured_programming [ ^ ]

https://en.wikipedia.org/wiki/Edsger_W._Dijkstra [ ^ ]

https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF [ ^ ]



建议:按字母顺序排序字谜,它可以帮助你完成一个方案。


As programmer, your job is to create algorithms that solve specific problems and you can't rely on someone else to eternally do it for you, so there is a time where you will have to learn how to. And the sooner, the better.

Take a sheet of paper and a pencil. Do some small samples A, AB, ABC, ABCD ...
At first, don't repeat letters in same word.
Think at how you can describe mechanically how you do to find all anagrams.
Once you have an algorithm that work, refine to handle repeated letters.

- Learn one or more analyze methods, E.W. Djikstra top-Down method is a good start.
https://en.wikipedia.org/wiki/Top-down_and_bottom-up_design[^]
https://en.wikipedia.org/wiki/Structured_programming[^]
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra[^]
https://www.cs.utexas.edu/users/EWD/ewd03xx/EWD316.PDF[^]

Advice: sort anagrams in alphabetical order, it may help you to fins a scheme.


我们做不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是像你想的那么难!所以从谷歌开始,找到算法,然后考虑实施。



如果遇到具体问题,请询问相关问题,我们会尽力帮助。但我们不打算为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think! So start with Google and find an algorithm, then think about implementation.

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


这篇关于如何编写函数以任何顺序返回给定单词(包括单词本身)的所有字谜。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆