Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Python - Filter Similar Case Strings
Next article icon

Python Program to check for almost similar Strings

Last Updated : 02 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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 strings test_str1 = 'aabcdaa' test_str2 = "abbaccd"  # printing original strings print("The original string 1 is : " + str(test_str1)) print("The original string 2 is : " + str(test_str2))  # initializing K K = 2  # getting frequencies freqs_1 = get_freq(test_str1) freqs_2 = get_freq(test_str2)  # checking for frequencies res = True for char in ascii_lowercase:     if abs(freqs_1[char] - freqs_2[char]) > K:         res = False         break  # printing result print("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 strings test_str1 = 'aabcdaa' test_str2 = "abbaccd"  # printing original strings print("The original string 1 is : " + str(test_str1)) print("The original string 2 is : " + str(test_str2))  # initializing K K = 2  # extracting frequencies cnt1 = Counter(test_str1.lower()) cnt2 = Counter(test_str2.lower())  # getting maximum difference res = True if max((cnt1 - cnt2).values()) > K or max((cnt2 - cnt1).values()) > K:     res = False  # printing result print("Are strings similar ? : " + str(res)) 

Output:

The original string 1 is : aabcdaa

The original string 2 is : abbaccd

Are strings similar ? : True

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3 : Using list comprehension:

Approach:

In this program, we define a function is_almost_similar_using_list_comprehension that takes three parameters: test_str1, test_str2, and k.

  • First, we create two lists counter1 and counter2 using list comprehension, which count the occurrences of each character in the respective strings.
  • Next, we calculate the absolute difference between the counts of each character in both lists using a for loop and sum() function.
  • Finally, we check if the total difference is less than or equal to k and return True or False accordingly.
Python3
def is_almost_similar_using_list_comprehension(test_str1, test_str2, k):     counter1 = [test_str1.count(char) for char in set(test_str1)]     counter2 = [test_str2.count(char) for char in set(test_str2)]     diff = sum(abs(counter1[i] - counter2[i]) for i in range(len(counter1)))     return diff >= k  # Testing test_str1 = 'aabcdaaa' test_str2 = 'abbaccda' k = 3 print(f"Input strings: '{test_str1}', '{test_str2}'") print(f"Value of K: {k}") print(f"Output: {is_almost_similar_using_list_comprehension(test_str1, test_str2, k)}") 

Output
Input strings: 'aabcdaaa', 'abbaccda' Value of K: 3 Output: True

The time complexity of the is_almost_similar_using_list_comprehension function is O(n), where n is the length of the longer string between test_str1 and test_str2.
The space complexity of the function is also O(n), since we are using two lists counter1 and counter2 to store the character counts of the respective strings.

Method 3: Using set() and count()

  • Create a set of unique characters in the first string using set(test_str1).
  • For each unique character in the set, count its frequency in both strings using count() method.
  • If the absolute difference in frequencies is greater than K for any character, then the strings are not similar.
Python3
# function to compute frequencies def are_strings_similar(test_str1, test_str2, K):     # getting unique characters in test_str1     unique_chars = set(test_str1)      # checking for frequencies     for char in unique_chars:         freq1 = test_str1.count(char)         freq2 = test_str2.count(char)         if abs(freq1 - freq2) > K:             return False     return True  # initializing strings test_str1 = 'aabcdaa' test_str2 = "abbaccd"  # printing original strings print("The original string 1 is : " + str(test_str1)) print("The original string 2 is : " + str(test_str2))  # initializing K K = 2  # checking if strings are similar res = are_strings_similar(test_str1, test_str2, K)  # printing result print("Are strings similar ? : " + str(res)) 

Output
The original string 1 is : aabcdaa The original string 2 is : abbaccd Are strings similar ? : True

Time Complexity: O(n^2), due to the time complexity of the are_strings_similar() function. 
Auxiliary Space: O(k), where k is the number of unique characters in test_str1. 


Next Article
Python - Filter Similar Case Strings
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

Similar Reads

  • Python program to check a string for specific characters
    Here, will check a string for a specific character using different methods using Python. In the below given example a string 's' and char array 'arr', the task is to write a python program to check string s for characters in char array arr. Examples: Input: s = @geeksforgeeks% arr[] = {'o','e','%'}O
    4 min read
  • Python Program To Check If A String Is Substring Of Another
    Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :  Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
    3 min read
  • Python - Filter Similar Case Strings
    Given the Strings list, the task is to write a Python program to filter all the strings which have a similar case, either upper or lower. Examples: Input : test_list = ["GFG", "Geeks", "best", "FOr", "all", "GEEKS"] Output : ['GFG', 'best', 'all', 'GEEKS'] Explanation : GFG is all uppercase, best is
    9 min read
  • Python | Strings with similar front and rear character
    Sometimes, while programming, we can have a problem in which we need to check for the front and rear characters of each string. We may require to extract the count of all strings with similar front and rear characters. Let's discuss certain ways in which this task can be performed. Method #1: Using
    4 min read
  • Python Program to Split joined consecutive similar characters
    Given a String, our task is to write a Python program to split on the occurrence of a non-similar character. Input : test_str = 'ggggffggisssbbbeessssstt'Output : ['gggg', 'ff', 'gg', 'i', 'sss', 'bbb', 'ee', 'sssss', 'tt']Explanation : All similar consecutive characters are converted to separate st
    5 min read
  • Python - Similar characters Strings comparison
    Given two Strings, separated by delim, check if both contain same characters. Input : test_str1 = 'e!e!k!s!g', test_str2 = 'g!e!e!k!s', delim = '!' Output : True Explanation : Same characters, just diff. positions. Input : test_str1 = 'e!e!k!s', test_str2 = 'g!e!e!k!s', delim = '!' Output : False Ex
    6 min read
  • Check for ASCII String - Python
    To check if a string contains only ASCII characters, we ensure all characters fall within the ASCII range (0 to 127). This involves comparing each character's value to ensure it meets the criteria. Using str.isascii()The simplest way to do this in Python is by using the built-in str.isascii() method
    2 min read
  • Python program to Extract Mesh matching Strings
    Given a character mesh, containing missing characters, match the string which matches the mesh. Example: Input : test_list = ["geeks", "best", "peeks"], mesh = "_ee_s" Output : ['geeks', 'peeks'] Explanation : Elements according to mesh are geeks and peeks. Input : test_list = ["geeks", "best", "tes
    5 min read
  • Python program to check if given string is pangram
    The task is to check if a string is a pangram which means it includes every letter of the English alphabet at least once. In this article, we’ll look at different ways to check if the string contains all 26 letters. Using Bitmasking Bitmasking uses a number where each bit represents a letter in the
    3 min read
  • Python program to check if a given string is Keyword or not
    This article will explore how to check if a given string is a reserved keyword in Python. Using the keyword We can easily determine whether a string conflicts with Python's built-in syntax rules. Using iskeyword()The keyword module in Python provides a function iskeyword() to check if a string is a
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences