-
Notifications
You must be signed in to change notification settings - Fork 22
/
Solution.java
63 lines (53 loc) · 1.47 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Problem: https://www.hackerrank.com/challenges/java-anagrams
// Difficulty: Easy
// Score: 10
import java.util.Scanner;
public class Solution {
static boolean isAnagram(String a, String b) {
int[] chars = new int[26];
if (a.length() != b.length()) {
return false;
}
// we can use this combine loop but when someone enter CAT and act both string
// in different case at that time this dosen't work
/*
* for (int i = 0, j = 0; i < a.length() && j < b.length(); i++, j++) { int i1 =
* (int) a.charAt(i); int i2 = (int) b.charAt(i); if (65 <= i1 && i1 <= 90 && 65
* <= i2 && 65 <= i2) { i1 += 32; i2 += 32; } i1 -= 97; chars[i1]++; i2 -= 97;
* chars[i2]--; }
*/
// In this you can pass any type of case like cAt and aCT anything
for (int i = 0; i < a.length(); i++) {
int i1 = (int) a.charAt(i);
// convert from upper to lowers
if (65 <= i1 && 90 >= i1) {
i1 += 32;
}
i1 -= 97;
chars[i1]++;
}
for (int i = 0; i < b.length(); i++) {
int i2 = (int) b.charAt(i);
// convert from upper to lowers
if (65 <= i2 && 90 >= i2) {
i2 += 32;
}
i2 -= 97;
chars[i2]--;
}
for (int i = 0; i < 26; i++) {
if (chars[i] != 0) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
scan.close();
boolean ret = isAnagram(a, b);
System.out.println((ret) ? "Solution" : "Not Solution");
}
}