zoukankan      html  css  js  c++  java
  • 给定一个单词,从字典查找该单词的所有兄弟单词。

         最近在网上看到百度的一个面试题:一个单词单词字母交换,可得另一个单词,如army->mary,成为兄弟单词。提供一个单词,在字典中找到它的兄弟。描述数据结构和查询过程。

         我的思路是这样的,所谓A单词是B单词的兄弟单词,无非就是组成A和B两个单词的所有字母都是一样,无非就是顺序不一样罢了。因此只要把单词按照AscII值来进行排序,然后判断下两个拍好序的单词是否完全一样,如果完全一样就是兄弟单词,否则就不是。下面是我用C#实现的代码。供参考,欢迎大家提供更好的思路。

          

           static void Main(string[] args)

            { 

                string[] Vocabularys = { "abcd", "ab", "army", "mary", "mnay", "mray", "myar", "rmay", "abcd" };

                Vocabularys = GetBrotherVocabularys(Vocabularys, "army");

                Console.WriteLine("the vocabularies of army's brother vocabulary are :");

                foreach (string vocabulary in Vocabularys)

                {

                    Console.Write("{0} ", vocabulary);

                }

                Console.Read();

            }

            /// <summary>

            /// 把一个字符串单词转化为一个字符列表

            /// </summary>

            /// <param name="vocabulary"></param>

            /// <returns></returns>

            private static IList<char> GetCharListByVoca(string vocabulary)

            {

                return (from m in vocabulary.ToCharArray() orderby m ascending select m).ToList();

            }

     

            /// <summary>

            /// 给定一个字符串数组,返回该数组中的是给定字符串的兄弟字符串的那些字符串

            /// </summary>

            /// <param name="vocabularys">字符串数组</param>

            /// <param name="keyVocabulary">给定字符串</param>

            /// <returns></returns>

            private static string[] GetBrotherVocabularys(string[] vocabularys, string keyVocabulary)

            {

                List<string> lst = new List<string>();

                IList<char> keyList = GetCharListByVoca(keyVocabulary);

                int len = keyVocabulary.Length;

                foreach (string item in vocabularys)

                {

                    if (item.Length == len)

                    {

                        if (IsBrotherVoca(item, keyList))

                        {

                            lst.Add(item);

                        }

                    }

                }

                return lst.ToArray();

            }

     

            private static bool IsBrotherVoca(string vocabulary, IList<char> keyVocaCharList)

            {

                IList<char> vocaCharList = GetCharListByVoca(vocabulary);        

                int i = 0;

                bool flag = true;

                foreach (char c in vocaCharList)

                {

                    if (c != keyVocaCharList[i])

                    {

                        flag = false;

                        break;

                    }

                    i++;

                }

                return flag;

            }

     

     

      运行结果如下:

  • 相关阅读:
    动手做第一个Chrome插件
    Discuz NT 架构剖析之Config机制
    用游标实现查询当前服务器所有数据库所有表的SQL
    Discuz X3.2 网站快照被劫持的解决方法
    centos下MYSQL 没有ROOT用户的解决方法。
    redis命令1
    在当今快节奏的软件更迭当中,我们是否还需要进行系统的学习?
    StructureMap 代码分析之Widget 之Registry 分析 (1)
    C#面试题汇总(未完成)
    C#:解决WCF中服务引用 自动生成代码不全的问题。
  • 原文地址:https://www.cnblogs.com/shaosks/p/2487602.html
Copyright © 2011-2022 走看看