Last active
July 26, 2020 01:36
-
-
Save idiotleon/d2185e5813b74816b4bab0747a377a6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SolutionAlmostEquivalentStrings{ | |
public String[] almostEquivalent(String[] s, String[] t){ | |
// sanity check | |
if(s == null || t == null || s.length != t.length) return new String[0]; | |
final int N = s.length; | |
String[] ans = new String[N]; | |
traversal: for(int i = 0; i < N; ++i){ | |
int[] freq = new int[26]; | |
String word1 = s[i], word2 = t[i]; | |
for(char ch : word1.toCharArray()) ++freq[ch - 'a']; | |
for(char ch : word2.toCharArray()){ | |
if(Math.abs(--freq[ch - 'a']) > 3) { | |
ans[i] = "No"; | |
// a little pruning here | |
continue traversal; | |
} | |
} | |
ans[i] = "Yes"; | |
} | |
return ans; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment