Finding is strings can be anagrams

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>s1 = safddadfs
s2 = famafmss
Result = True
</code>
<code>s1 = safddadfs s2 = famafmss Result = True </code>
s1 = safddadfs
s2 = famafmss

Result = True

Explanation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>s1 = aaabb
s2 = aabbb
Result = True
</code>
<code>s1 = aaabb s2 = aabbb Result = True </code>
s1 = aaabb
s2 = aabbb

Result = True

Explanation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>s1 = aab
s2 = ab
Result = True
</code>
<code>s1 = aab s2 = ab Result = True </code>
s1 = aab
s2 = ab

Result = True

Explanation:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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]
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật