zoukankan      html  css  js  c++  java
  • use isSubstring to check if one word is a rotation of another.

       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:      }
  • 相关阅读:
    栈大小和内存分部问题
    inline和宏之间的区别
    两个栈实现双端队列
    Hibernate学习笔记-Hibernate关系映射
    Hibernate学习笔记-Hibernate HQL查询
    Hibernate学习笔记--第一个Hibernate框架程序
    Hibernate学习笔记--Hibernate框架错误集合及解决
    Java学习笔记--对象克隆
    Scala学习笔记--文件IO
    Java学习笔记--Socket和ServerSocket
  • 原文地址:https://www.cnblogs.com/dancewithautomation/p/3513520.html
Copyright © 2011-2022 走看看