Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class StringOps
- {
- /**
- * precondition: s1 and s2 cannot be null
- * @param s1 string 1
- * @param s2 string 2
- * @return true if s1 & s2 are anagrams – false otherwise
- */
- public static boolean AreAnagrams(String s1, String s2)
- {
- if (s1 == null || s2 == null)
- throw new IllegalArgumentException ("null string");
- if (s1.length() != s2.length())
- return false;
- String one;
- String two;
- char[] three;
- char[] four;
- one = s1;
- two = s2;
- one = one.toLowerCase();
- two = two.toLowerCase();
- three = one.toCharArray();
- four = two.toCharArray();
- Arrays.sort(three);
- Arrays.sort(four);
- boolean num = false;
- if (three.length != four.length)
- return false;
- if (s1 == s2)
- return true;
- for(int i = 0; i < three.length; i++)
- if (three[i] == four[i])
- num = true;
- return num;
- }
- /**
- * precondition: s1 and s2 cannot be null
- * @param s1 string 1
- * @param s2 string 2
- * @return return true if s1 is a substring of s2 – false otherwise
- */
- public static boolean isSubstring(String s1, String s2)
- {
- if (s1 == null || s2 == null)
- throw new IllegalArgumentException ("null string");
- int count = 0;
- boolean nums = true;
- if (s1.length() == s2.length())
- return true;
- if (s1.length() < s2.length())
- for(int i = 0; i < s2.length()- s1.length() || nums; i++)
- {
- System.out.print(s2.substring(i,i+s1.length()));
- System.out.println(s1.length());
- if(s2.substring(i,i+s1.length()).equals(s1))
- nums = true;
- }
- else
- return false;
- return nums;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement