use isSubstring to check if one word is a rotation of another.

时间:2023-12-26 17:49:55

 

   1:      /// <summary>
   2:      /// Assume you have a method isSubstring which checks if one word is a substring of another. 
   3:      /// Given two strings, s1 and s2, 
   4:      /// write code to check if s2 is a rotation of s1 using only one call to 
   5:      /// isSubstring (i.e., “waterbottle” is a rotation of “erbottlewat”).
   6:      /// </summary>
   7:      class Program
   8:      {
   9:          static void Main(string[] args)
  10:          {
  11:              string s1 = "waterbottle";
  12:              string s2 = "erbottlewat";
  13:              Program p = new Program();
  14:              bool r = p.IsARotation(s1, s2);
  15:          }
  16:   
  17:          public bool IsARotation(string s1, string s2)
  18:          {
  19:              if (string.IsNullOrEmpty(s1) || string.IsNullOrEmpty(s2))
  20:              {
  21:                  throw new ArgumentNullException("please do not input empty or null string");
  22:              }
  23:   
  24:              if (s1.Length != s2.Length)
  25:              {
  26:                  return false;
  27:              }
  28:   
  29:              string ns = s1 + s1;
  30:   
  31:              return ns.Contains(s2);
  32:          }
  33:      }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }