Nguythang

Anagram

Feb 23rd, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package workshop06;
  7.  
  8. import java.util.Arrays;
  9.  
  10. /**
  11.  *
  12.  * @author NgT
  13.  */
  14. public class Ex3 {
  15.  
  16.     static void isAnagram(String s1, String s2) {
  17.         String copyOfs1 = s1.replaceAll("\\s", "");
  18.         String copyOfs2 = s2.replaceAll("\\s", "");
  19.  
  20.         boolean status = true;
  21.  
  22.         if (copyOfs1.length() != copyOfs2.length()) {
  23.             status = false;
  24.         } else {
  25.             char[] s1Array = copyOfs1.toLowerCase().toCharArray();
  26.             char[] s2Array = copyOfs2.toLowerCase().toCharArray();
  27.             Arrays.sort(s1Array);
  28.             Arrays.sort(s2Array);
  29.             status = Arrays.equals(s1Array, s2Array);
  30.         }
  31.         if (status) {
  32.             System.out.println(s1 + " and " + s2 + " are anagrams");
  33.         } else {
  34.             System.out.println(s1 + " and " + s2 + " are not anagrams");
  35.         }
  36.     }
  37.  
  38.     public static void main(String[] args) {
  39.         isAnagram("parliament", "partial men,");
  40.         isAnagram("software", "swear oft.");
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment