Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4. public class StringOps
  5. {
  6.  
  7. /**
  8. * precondition: s1 and s2 cannot be null
  9. * @param s1 string 1
  10. * @param s2 string 2
  11. * @return true if s1 & s2 are anagrams – false otherwise
  12. */
  13. public static boolean AreAnagrams(String s1, String s2)
  14. {
  15. if (s1 == null || s2 == null)
  16. throw new IllegalArgumentException ("null string");
  17.  
  18. if (s1.length() != s2.length())
  19. return false;
  20.  
  21. String one;
  22. String two;
  23. char[] three;
  24. char[] four;
  25.  
  26. one = s1;
  27. two = s2;
  28.  
  29. one = one.toLowerCase();
  30. two = two.toLowerCase();
  31.  
  32. three = one.toCharArray();
  33. four = two.toCharArray();
  34. Arrays.sort(three);
  35. Arrays.sort(four);
  36. boolean num = false;
  37.  
  38. if (three.length != four.length)
  39. return false;
  40. if (s1 == s2)
  41. return true;
  42.  
  43. for(int i = 0; i < three.length; i++)
  44. if (three[i] == four[i])
  45. num = true;
  46.  
  47. return num;
  48. }
  49.  
  50. /**
  51. * precondition: s1 and s2 cannot be null
  52. * @param s1 string 1
  53. * @param s2 string 2
  54. * @return return true if s1 is a substring of s2 – false otherwise
  55. */
  56. public static boolean isSubstring(String s1, String s2)
  57. {
  58. if (s1 == null || s2 == null)
  59. throw new IllegalArgumentException ("null string");
  60. int count = 0;
  61. boolean nums = true;
  62.  
  63. if (s1.length() == s2.length())
  64. return true;
  65.  
  66. if (s1.length() < s2.length())
  67.  
  68. for(int i = 0; i < s2.length()- s1.length() || nums; i++)
  69. {
  70. System.out.print(s2.substring(i,i+s1.length()));
  71. System.out.println(s1.length());
  72. if(s2.substring(i,i+s1.length()).equals(s1))
  73. nums = true;
  74.  
  75. }
  76. else
  77. return false;
  78.  
  79.  
  80.  
  81. return nums;
  82. }
  83.  
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement