Skip to content

Instantly share code, notes, and snippets.

@idiotleon
Last active July 26, 2020 01:36
Show Gist options
  • Save idiotleon/d2185e5813b74816b4bab0747a377a6b to your computer and use it in GitHub Desktop.
Save idiotleon/d2185e5813b74816b4bab0747a377a6b to your computer and use it in GitHub Desktop.
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