forked from anthonynsimon/java-ds-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPermutationMatch.java
More file actions
31 lines (24 loc) · 784 Bytes
/
PermutationMatch.java
File metadata and controls
31 lines (24 loc) · 784 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
25
26
27
28
29
30
31
package com.anthonynsimon.algorithms.strings;
public final class PermutationMatch {
// Returns true if one string is a permutation of another
// Time O(N)
public static boolean isPermutation(String strA, String strB) {
if (strA.length() != strB.length()) {
return false;
}
// Assume it's regular ASCII
int[] chars = new int[128];
// Count chars in A (case sensitive)
for (int i = 0; i < strA.length(); i++) {
chars[strA.charAt(i)] += 1;
}
// Count chars in B
for (int i = 0; i < strB.length(); i++) {
chars[strB.charAt(i)] -= 1;
if (chars[strB.charAt(i)] < 0) {
return false;
}
}
return true;
}
}