-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path_1_2.java
More file actions
24 lines (22 loc) · 714 Bytes
/
_1_2.java
File metadata and controls
24 lines (22 loc) · 714 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.fisher.coder.chapter1;
/**
* Created by stevesun on 4/16/17.
*/
public class _1_2 {
/**Given two strings, write a method to decide if one is a permuation of the other.*/
public static boolean isPermutation(String a, String b) {
if (a.length() != b.length()) return false;
//ask your interviewer if the two strings are ASCII first before you use 256 as the array length
int[] count = new int[256];
for (char c : a.toCharArray()) {
count[c - 'a']++;
}
for (char c : b.toCharArray()) {
count[c - 'a']--;
}
for (int i : count) {
if (i != 0) return false;
}
return true;
}
}