mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-09 15:59:06 +01:00
cs: add “1657. Determine if Two Strings Are Close”
Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
34acc09b98
commit
8171cd6289
1 changed files with 33 additions and 0 deletions
33
cs/determine-if-two-strings-are-close.cs
Normal file
33
cs/determine-if-two-strings-are-close.cs
Normal file
|
@ -0,0 +1,33 @@
|
||||||
|
public class Solution {
|
||||||
|
private static List<int> Freqs(string word) {
|
||||||
|
var f = new List<int>(Enumerable.Repeat(0, 26));
|
||||||
|
|
||||||
|
foreach (var c in word) {
|
||||||
|
++f[c - 'a'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return f;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int Mask(List<int> f) {
|
||||||
|
var m = 0;
|
||||||
|
|
||||||
|
foreach (var c in f) {
|
||||||
|
m = (m << 1) | (c > 0 ? 1 : 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool CloseStrings(string word1, string word2) {
|
||||||
|
var f1 = Freqs(word1);
|
||||||
|
var m1 = Mask(f1);
|
||||||
|
f1.Sort();
|
||||||
|
|
||||||
|
var f2 = Freqs(word2);
|
||||||
|
var m2 = Mask(f2);
|
||||||
|
f2.Sort();
|
||||||
|
|
||||||
|
return m1 == m2 && f1.SequenceEqual(f2);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue