判断一字符串是否可以另一字符串重新排列而成

   1:      class Program
   2:      {
   3:          static void Main(string[] args)
   4:          {
   5:              string s = File.ReadAllText(@"e:	est.txt");
   6:              Program p = new Program();
   7:              bool r=p.IsAnagrams_1(s,s);
   8:              r = p.IsAnagrams_2(s,s);
   9:          }
  10:   
  11:          /// <summary>
  12:          /// Check if the two strings have identical counts for each unique char.
  13:          /// 首先使用框架自带函数OrderByDescending,稍后会作比较
  14:          /// 因为使用了背后机制为快速排序,所以复杂度为O(nLogn)
  15:          /// </summary>
  16:          public bool IsAnagrams_1(string s1, string s2)
  17:          {
  18:              if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s1))
  19:              { 
  20:                  throw new ArgumentException("not accept null or empty string");
  21:              }
  22:              else if (s1.Length != s1.Length)
  23:              {
  24:                  return false;
  25:              }
  26:   
  27:              //OrderByDescending背后用的是快速排序法
  28:              char[] charArray1 = s1.OrderByDescending(a => a).ToArray();
  29:              char[] charArray2 = s2.OrderByDescending(a => a).ToArray();
  30:   
  31:              for (int i = 0; i < charArray1.Length; i++)
  32:              {
  33:                  if (charArray1[i] != charArray2[i])
  34:                  {
  35:                      return false;
  36:                  }
  37:              }
  38:              return true;
  39:          }
  40:   
  41:          /// <summary>
  42:          /// 该方法依旧假设输入为ASCII串
  43:          /// 复杂度为O(n)
  44:          /// </summary>
  45:          public bool IsAnagrams_2(string s1, string s2)
  46:          {
  47:              if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s1))
  48:              {
  49:                  throw new ArgumentException("not accept null or empty string");
  50:              }
  51:              else if (s1.Length != s1.Length)
  52:              {
  53:                  return false;
  54:              }
  55:   
  56:              int[] flags = new int[256];
  57:              for (int i = 0; i < s1.Length; i++)
  58:              {
  59:                  char c = s1[i];
  60:                  if (c > 255)
  61:                  {
  62:                      throw new ArgumentException("not accept non-ASCII string");
  63:                  }
  64:   
  65:                  flags[c]++;
  66:              }
  67:   
  68:              for (int i = 0; i < s2.Length; i++)
  69:              {
  70:                  char c = s2[i];
  71:                  if (c > 255)
  72:                  {
  73:                      throw new ArgumentException("not accept non-ASCII string");
  74:                  }
  75:   
  76:                  if (flags[c] == 0)
  77:                  {
  78:                      return false;
  79:                  }
  80:                  flags[c]--;
  81:              }
  82:   
  83:              return true;
  84:          }
  85:   
  86:          /****
  87:           因为复杂度不在一个等级上,所以在处理超长字符串的时候,2比1要快很多,百万级别长度字串实验验证如下
  88:           但是2因为对输入要求限制较高,所以影响了其应用,实践中,如果是超长未知格式字符串,不妨先尝试用法2试探
  89:           如果ASCII占大多数,不妨分而治之
  90:           ****/
  91:      }

N@){CL56D8ZL9GP64K~$PFI

原文地址:https://www.cnblogs.com/dancewithautomation/p/3494723.html