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;

            }

     

     

      运行结果如下:

  • 相关阅读:
    ngx_lua_waf完整安装说明
    Linux(CentOS)下的JDK的安装和环境配置
    Genymotion的2个问题及解决方法
    Appscan的第一个测试请求就是提交MAC地址
    oracle相关知识
    数据结构之树
    kafka的写入内存?硬盘
    算法的时间复杂度和空间复杂度
    Java线程池
    mapReduce和spark的shuffle
  • 原文地址:https://www.cnblogs.com/shaosks/p/2487602.html
Copyright © 2011-2022 走看看