Check Whether Two Strings are Almost Equivalent

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
Two strings word1 and word2 are considered almost equivalent if the differences between the frequencies of each letter from 'a' to 'z' between word1 and word2 is at most 3.
Given two strings word1 and word2, each of length n, return true if word1 and word2 are almost equivalent*, or false otherwise*.
The frequency of a letter x is the number of times it occurs in the string.
LeetCode Problem - 2068
class Solution {
public boolean checkAlmostEquivalent(String word1, String word2) {
// Create a map to store the frequency differences of characters.
Map<Character, Integer> frequencyMap = new HashMap<>();
// Count frequencies for characters in word1
for (char ch : word1.toCharArray()) {
frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) + 1);
// Increment the count for the character in the map
}
// Subtract frequencies for characters in word2
for (char ch : word2.toCharArray()) {
frequencyMap.put(ch, frequencyMap.getOrDefault(ch, 0) - 1);
// Decrement the count for the character in the map
}
// Check if the absolute difference for any character exceeds 3
for (int freq : frequencyMap.values()) {
if (Math.abs(freq) > 3) {
// If the frequency difference is greater than 3, the words are not almost equivalent
return false;
}
}
// If no character's frequency difference exceeds 3, the words are almost equivalent
return true;
}
}




