String Question 14

Home / Java Practice Question / String Question 14

String Question 14


Question 24. Write a Program to check whether two strings are almost equivalent or not.(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.)

Program


class Solution {

    public boolean checkAlmostEquivalent(String word1, String word2) 

    {

       int[] alphabets = new int[26];

        int len = word1.length();      

        for(int i=0; i<len;i++)

        {

            alphabets[word1.charAt(i) - 'a']++;

            alphabets[word2.charAt(i) - 'a']--;

        }        

        for(int i=0; i<26;i++)

        {

            if(Math.abs(alphabets[i]) > 3)

                return false;

        }

        return true;

    }

}