repl.it
@shachopin/

Valid Anagram

Java

No description

fork
loading
Files
  • Main.java
  • jdt.ls-java-project
Main.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
class Main {
  public static void main(String[] args) {
    System.out.println("Hello world!");
  }
}

MySolution on lintcode 158 Valid Anagram
public class Solution {
    /**
     * @param s: The first string
     * @param t: The second string
     * @return: true or false
     */
    public boolean anagram(String s, String t) {
        // write your code here
        int[] countS = new int[256];
        int[] countT = new int[256];
        char[] sChars = s.toCharArray();
        char[] tChars = t.toCharArray();
        for (char sChar : sChars) {
            countS[sChar]++;
        }
        
        for (char tChar : tChars) {
            countT[tChar]++;
        }
        
        for (int i = 0; i < 256; i++) {
            if (countS[i] != countT[i]) {
                return false;
            }
        }
        return true;
    }
}