I have String s1
and String s2
, the pair s1
, s2
is called special strings if we can make them anagram by removing a single character (any number of times) from each string s1
and s2
.
Example:
<code>s1 = safddadfs
s2 = famafmss
Result = True
</code>
<code>s1 = safddadfs
s2 = famafmss
Result = True
</code>
s1 = safddadfs
s2 = famafmss
Result = True
Explanation:
<code>s1 has a=2, d=2, f=2, s=2
s2 has a=2, d=2, m=2, s=2
We can remove 'f' from s1 and remove 'm' from s2 to make them anagrams.
so s1 becomes a=2, d=2, s=2
and s2 becomes a=2, d=2, s=2
</code>
<code>s1 has a=2, d=2, f=2, s=2
s2 has a=2, d=2, m=2, s=2
We can remove 'f' from s1 and remove 'm' from s2 to make them anagrams.
so s1 becomes a=2, d=2, s=2
and s2 becomes a=2, d=2, s=2
</code>
s1 has a=2, d=2, f=2, s=2
s2 has a=2, d=2, m=2, s=2
We can remove 'f' from s1 and remove 'm' from s2 to make them anagrams.
so s1 becomes a=2, d=2, s=2
and s2 becomes a=2, d=2, s=2
Example:
<code>s1 = aaabb
s2 = aabbb
Result = True
</code>
<code>s1 = aaabb
s2 = aabbb
Result = True
</code>
s1 = aaabb
s2 = aabbb
Result = True
Explanation:
<code>s1 has a=3, b=2
s2 has a=2, b=3
We can single 'a' from s1 and remove single 'b' from s2 to make them anagrams.
so s1 becomes a=2, b=2
and s2 becomes a=2, b=2
</code>
<code>s1 has a=3, b=2
s2 has a=2, b=3
We can single 'a' from s1 and remove single 'b' from s2 to make them anagrams.
so s1 becomes a=2, b=2
and s2 becomes a=2, b=2
</code>
s1 has a=3, b=2
s2 has a=2, b=3
We can single 'a' from s1 and remove single 'b' from s2 to make them anagrams.
so s1 becomes a=2, b=2
and s2 becomes a=2, b=2
Example:
<code>s1 = aab
s2 = ab
Result = True
</code>
<code>s1 = aab
s2 = ab
Result = True
</code>
s1 = aab
s2 = ab
Result = True
Explanation:
<code>s1 has a=2, b=1
s2 has a=1, b=1
We can single 'a' from s1 and nothing from s2 to make them anagrams.
so s1 becomes a=1, b=1
and s2 becomes a=1, b=1
</code>
<code>s1 has a=2, b=1
s2 has a=1, b=1
We can single 'a' from s1 and nothing from s2 to make them anagrams.
so s1 becomes a=1, b=1
and s2 becomes a=1, b=1
</code>
s1 has a=2, b=1
s2 has a=1, b=1
We can single 'a' from s1 and nothing from s2 to make them anagrams.
so s1 becomes a=1, b=1
and s2 becomes a=1, b=1
I tried implementing the code for this, but it is failing for the test case where s1 = “aaabb” and s2 = “aabb”
I am not able to understand the correct logic to fix this.
<code>import java.util.*;
public class Main {
public static boolean solve(String s1, String s2) {
Map<Character, Integer> m1 = getCountMap(s1);
Map<Character, Integer> m2 = getCountMap(s2);
boolean valid = canBeAnagrams(m1, m2);
if(!valid) {
valid = canBeAnagrams(m2, m1);
}
return valid;
}
private static boolean canBeAnagrams(Map<Character, Integer> m3, Map<Character, Integer> m4) {
int count = 0;
Map<Character, Integer> m1 = new HashMap<>(m3), m2 = new HashMap<>(m4);
for(char c : m1.keySet()) {
int v1 = m1.get(c);
int v2 = m2.getOrDefault(c,0);
if(v1 != v2) {
count++;
}
m2.remove(c);
if(count > 1) return false;
}
if(m2.size() > 1) return false;
return true;
}
private static Map<Character, Integer> getCountMap(String s) {
Map<Character, Integer> countMap = new HashMap<>();
for (char c : s.toCharArray()) {
countMap.put(c, countMap.getOrDefault(c, 0) + 1);
}
return countMap;
}
public static void main(String[] args) {
System.out.println(solve("safddadfs", "famafmss")); // Should print [True]
System.out.println(solve("aaabb", "aabbb")); // Should print [True], but getting false
System.out.println(solve("aab", "ab")); // Should print [True]
}
}
</code>
<code>import java.util.*;
public class Main {
public static boolean solve(String s1, String s2) {
Map<Character, Integer> m1 = getCountMap(s1);
Map<Character, Integer> m2 = getCountMap(s2);
boolean valid = canBeAnagrams(m1, m2);
if(!valid) {
valid = canBeAnagrams(m2, m1);
}
return valid;
}
private static boolean canBeAnagrams(Map<Character, Integer> m3, Map<Character, Integer> m4) {
int count = 0;
Map<Character, Integer> m1 = new HashMap<>(m3), m2 = new HashMap<>(m4);
for(char c : m1.keySet()) {
int v1 = m1.get(c);
int v2 = m2.getOrDefault(c,0);
if(v1 != v2) {
count++;
}
m2.remove(c);
if(count > 1) return false;
}
if(m2.size() > 1) return false;
return true;
}
private static Map<Character, Integer> getCountMap(String s) {
Map<Character, Integer> countMap = new HashMap<>();
for (char c : s.toCharArray()) {
countMap.put(c, countMap.getOrDefault(c, 0) + 1);
}
return countMap;
}
public static void main(String[] args) {
System.out.println(solve("safddadfs", "famafmss")); // Should print [True]
System.out.println(solve("aaabb", "aabbb")); // Should print [True], but getting false
System.out.println(solve("aab", "ab")); // Should print [True]
}
}
</code>
import java.util.*;
public class Main {
public static boolean solve(String s1, String s2) {
Map<Character, Integer> m1 = getCountMap(s1);
Map<Character, Integer> m2 = getCountMap(s2);
boolean valid = canBeAnagrams(m1, m2);
if(!valid) {
valid = canBeAnagrams(m2, m1);
}
return valid;
}
private static boolean canBeAnagrams(Map<Character, Integer> m3, Map<Character, Integer> m4) {
int count = 0;
Map<Character, Integer> m1 = new HashMap<>(m3), m2 = new HashMap<>(m4);
for(char c : m1.keySet()) {
int v1 = m1.get(c);
int v2 = m2.getOrDefault(c,0);
if(v1 != v2) {
count++;
}
m2.remove(c);
if(count > 1) return false;
}
if(m2.size() > 1) return false;
return true;
}
private static Map<Character, Integer> getCountMap(String s) {
Map<Character, Integer> countMap = new HashMap<>();
for (char c : s.toCharArray()) {
countMap.put(c, countMap.getOrDefault(c, 0) + 1);
}
return countMap;
}
public static void main(String[] args) {
System.out.println(solve("safddadfs", "famafmss")); // Should print [True]
System.out.println(solve("aaabb", "aabbb")); // Should print [True], but getting false
System.out.println(solve("aab", "ab")); // Should print [True]
}
}