biryu2205 / Biryu

0 stars 0 forks source link

Hackerrank-String-Anagram #70

Closed biryu2205 closed 6 years ago

biryu2205 commented 6 years ago
import java.util.Scanner;

public class Anagram {
  static boolean anagram(String string1, String string2) {
    int result = 0;
    if (string1.length() - string2.length() != 0) {
      result = 0;
    } else {
      char s1[] = string1.toCharArray();
      for (int i = 0; i < string1.length(); i++) {
        for (int j = 0; j < string1.length(); j++) {
          if (s1[i] > s1[j]) {
            char temp = s1[i];
            s1[i] = s1[j];
            s1[j] = temp;
          }
        }
      }

      char s2[] = string1.toCharArray();
      for (int i = 0; i < string2.length(); i++) {
        for (int j = 0; j < string2.length(); j++) {
          if (s2[i] > s2[j]) {
            char temp = s2[i];
            s2[i] = s2[j];
            s2[j] = temp;
          }
        }
      }
      for (int i = 0; i < string1.length(); i++) {
        if (s1[i] != s2[i]) {
          result = 0;
          break;
        } else {
          result = 1;
        }
      }
    }
    return result == 1;
  }

  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("insert 1st string");
    String string1 = sc.nextLine();
    System.out.println("insert 2nd string");
    String string2 = sc.nextLine();
    if (anagram(string1, string2)) {
      System.out.println("Anagram");
    } else {
      System.out.println("Not Anagram");
    }
  }
}