// Code language: Java classSolution { publicbooleanCheckPermutation(String s1, String s2) { int[] cnt = newint[256]; if (s1.length() != s2.length()) returnfalse; for (inti=0, n = s1.length(); i < n; ++i) cnt[s1.charAt(i)]++; for (inti=0, n = s2.length(); i < n; ++i) if (--cnt[s2.charAt(i)] < 0) returnfalse; for (inti=0; i < 256; ++i) if (cnt[i] != 0) returnfalse; returntrue; } }
1 2 3 4 5 6 7 8 9 10 11
// Code language: C++ classSolution { public: boolCheckPermutation(string s1, string s2){ int cnt[256]{0}; for (char c: s1) cnt[c]++; for (char c: s2) if (--cnt[c] < 0) returnfalse; for (int i = 0; i < 256; ++i) if (cnt[i] != 0) returnfalse; returntrue; } };