Python Program to check for almost similar Strings

02 Feb, 2021
Given two strings, the task here is to write a python program that can test if they are almost similar. Similarity of strings is being checked on the criteria of frequency difference of each character which should be greater than a threshold here represented by K.
Input : test_str1 = ‘aabcdaa’, test_str2 = “abbaccd”, K = 2
Output : True
Explanation : ‘a’ occurs 4 times in str1, and 2 times in str2, 4 – 2 = 2, in range, similarly, all chars in range, hence true.
Input : test_str1 = ‘aabcdaaa’, test_str2 = “abbaccda”, K = 3
Output : True
Explanation : ‘a’ occurs 5 times in str1, and 3 times in str2, 5 – 3 = 2, in range, similarly, all chars in range, hence true.
Method 1 : Using ascii_lowecase, dictionary comprehension, loop and abs()
In this, we compute all the frequencies of all the characters in both strings using dictionary comprehension and loop. Next, each character is iterated from alphabetic lowercase ascii characters and tested for frequency difference in both strings using abs(), if any difference computes to greater than K, result is flagged off.
Example
Python3
from string import ascii_lowercase  # function to compute frequencies    def get_freq(test_str):      # starting at 0 count    freqs = {char: 0 for char in ascii_lowercase}      # counting frequencies    for char in test_str:        freqs[char] += 1    return freqs    # initializing stringstest_str1 = 'aabcdaa'test_str2 = "abbaccd"  # printing original stringsprint("The original string 1 is : " + str(test_str1))print("The original string 2 is : " + str(test_str2))  # initializing KK = 2  # getting frequenciesfreqs_1 = get_freq(test_str1)freqs_2 = get_freq(test_str2)  # checking for frequenciesres = Truefor char in ascii_lowercase:    if abs(freqs_1[char] - freqs_2[char]) > K:        res = False        break  # printing resultprint("Are strings similar ? : " + str(res))
Output:
The original string 1 is : aabcdaa
The original string 2 is : abbaccd
Are strings similar ? : True
Method 2 : Using Counter() and max()
In this, we perform task of getting individual characters’ frequency using Counter() and get the maximum difference using max(), if greater than K, then result is flagged off.
Example:
Python3
from collections import Counter  # initializing stringstest_str1 = 'aabcdaa'test_str2 = "abbaccd"  # printing original stringsprint("The original string 1 is : " + str(test_str1))print("The original string 2 is : " + str(test_str2))  # initializing KK = 2  # extracting frequenciescnt1 = Counter(test_str1.lower())cnt2 = Counter(test_str2.lower())  # getting maximum differenceres = Trueif max((cnt1 - cnt2).values()) > K or max((cnt2 - cnt1).values()) > K:    res = False  # printing resultprint("Are strings similar ? : " + str(res))
Output:
The original string 1 is : aabcdaa
The original string 2 is : abbaccd
Are strings similar ? : True
Python string-programs
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Introduction To PYTHON
Python OOPs Concepts
Python | os.path.join() method
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python program to check whether a number is Prime or not
Python | Convert string dictionary to dictionary