Check whether Strings are k distance apart or not
Given two strings, the task is to find if they are only less than or equal to k edit distance apart. It means that strings are only k edit distance apart when there are only k mismatches.
Print Yes if there are less than or equal to k mismatches, Else No.
Also print yes if both strings are already same.
Examples :
Input : str1 = "geeksforgeeks"
str2 = "geeksforgeek"
k = 1
Output : Yes
Explanation: Only one character is mismatched
or to be removed i.e. s at last
Input : str1 = "nishant"
str2 = "nisha"
k = 1
Output : No
Explanation: 2 characters need to be removed
i.e. n and t
Input : str1 = "practice"
str2 = "prac"
k = 3
Output : No
Explanation: 4 characters are mismatched or to
be removed i.e. t, i, c, e at last i.e. > k
Input : str1 = "Ping" str2 = "Paging" k = 2
Output : Yes
Explanation: 2 characters need to be removed or
mismatched i.e. a and g in paging
Algorithm:
1- Check if the difference in the length of both strings is greater than k if so, return false.
2- Find edit distance of two strings. If edit distance is less than or equal to k, return true. Else return false.
C++
// A Dynamic Programming based C++ program to // find minimum number operations is less than // or equal to k or not to convert str1 to str2 #include <bits/stdc++.h> using namespace std; // Utility function to find minimum of three numbers int min(int x, int y, int z) { return min(min(x, y), z); } int editDistDP(string str1, string str2, int m, int n) { // Create a table to store results of subproblems int dp[m + 1][n + 1]; // Fill d[][] in bottom up manner for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { // If first string is empty, only option is to // insert all characters of second string if (i == 0) dp[i][j] = j; // Min. operations = j // If second string is empty, only option is to // remove all characters of second string else if (j == 0) dp[i][j] = i; // Min. operations = i // If last characters are same, ignore last char // and recur for remaining string else if (str1[i - 1] == str2[j - 1]) dp[i][j] = dp[i - 1][j - 1]; // If last character are different, consider all // possibilities and find minimum else dp[i][j] = 1 + min(dp[i][j - 1], // Insert dp[i - 1][j], // Remove dp[i - 1][j - 1]); // Replace } } return dp[m][n]; } // Returns true if str1 and str2 are k edit distance apart, // else false. bool areKDistant(string str1, string str2, int k) { int m = str1.length(); int n = str2.length(); if (abs(m - n) > k) return false; return (editDistDP(str1, str2, m, n) <= k); } // Driver program int main() { // your code goes here string str1 = "geek"; string str2 = "gks"; int k = 3; areKDistant(str1, str2, k) ? cout << "Yes" : cout << "No"; return 0; } |
Java
// A Dynamic Programming based Java program to // find minimum number operations is less than // or equal to k or not to convert str1 to str2 class GFG { // Utility function to find minimum // of three numbers static int min(int x, int y, int z) { return Math.min(Math.min(x, y), z); } static int editDistDP(String str1, String str2, int m, int n) { // Create a table to store // results of subproblems int dp[][] = new int[m + 1][n + 1]; // Fill d[][] in bottom up manner for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { // If first string is empty, // only option is to insert // all characters of second string if (i == 0) dp[i][j] = j; // Min. operations = j // If second string is empty, // only option is to remove // all characters of second string else if (j == 0) dp[i][j] = i; // Min. operations = i // If last characters are same, // ignore last char and recur // for remaining string else if (str1.charAt(i - 1) == str2.charAt(j - 1)) dp[i][j] = dp[i - 1][j - 1]; // If last character are different, // consider all possibilities // and find minimum else dp[i][j] = 1 + min(dp[i][j - 1], // Insert dp[i - 1][j], // Remove dp[i - 1][j - 1]); // Replace } } return dp[m][n]; } // Returns true if str1 and str2 are // k edit distance apart, else false. static boolean areKDistant(String str1, String str2, int k) { int m = str1.length(); int n = str2.length(); if (Math.abs(m - n) > k) return false; return (editDistDP(str1, str2, m, n) <= k); } // driver code public static void main(String[] args) { String str1 = "geek"; String str2 = "gks"; int k = 3; if (areKDistant(str1, str2, k)) System.out.print("Yes"); else System.out.print("No"); } } // This code is contributed by Anant Agarwal. |
Python3
# A Dynamic Programming based Python3 program to # find minimum number operations is less than # or equal to k or not to convert str1 to str2 # Utility function to find minimum # of three numbers def minn(x, y, z) : return min(min(x, y), z) def editDistDP(str1, str2, m, n) : # Create a table to store results # of subproblems dp = [[0 for i in range(n + 1)] for j in range(m + 1)] # Fill d[][] in bottom up manner for i in range(m + 1): for j in range(n + 1): # If first is empty, only option is # to insert all characters of second if (i == 0) : dp[i][j] = j # Min. operations = j # If second is empty, only option is # to remove all characters of second elif (j == 0): dp[i][j] = i # Min. operations = i # If last characters are same, # ignore last char and recur # for remaining elif (str1[i - 1] == str2[j - 1]): dp[i][j] = dp[i - 1][j - 1] # If last character are different, # consider all possibilities and # find minimum else: dp[i][j] = 1 + minn(dp[i][j - 1], # Insert dp[i - 1][j], # Remove dp[i - 1][j - 1]) # Replace return dp[m][n] # Returns true if str1 and str2 are # k edit distance apart, else false. def areKDistant(str1, str2, k): m = len(str1) n = len(str2) if (abs(m - n) > k) : return False return (editDistDP(str1, str2, m, n) <= k) # Driver Code if __name__ == '__main__': str1 = "geek" str2 = "gks" k = 3 if areKDistant(str1, str2, k): print("Yes") else: print("No") # This code is contributed # by SHUBHAMSINGH10 |
C#
// A Dynamic Programming based C# // program to find minimum number // operations is less than or equal // to k or not to convert str1 to str2 using System; class GFG { // Utility function to find minimum // of three numbers static int min(int x, int y, int z) { return Math.Min(Math.Min(x, y), z); } static int editDistDP(string str1, string str2, int m, int n) { // Create a table to store // results of subproblems int [,]dp = new int[m + 1, n + 1]; // Fill d[][] in bottom up manner for (int i = 0; i <= m; i++) { for (int j = 0; j <= n; j++) { // If first string is empty, // only option is to insert // all characters of second string if (i == 0) // Min. operations = j dp[i, j] = j; // If second string is empty, // only option is to remove // all characters of second string else if (j == 0) // Min. operations = i dp[i, j] = i; // If last characters are same, // ignore last char and recur // for remaining string else if (str1[i - 1] == str2[j - 1]) dp[i, j] = dp[i - 1,j - 1]; // If last character are different, // consider all possibilities // and find minimum else dp[i, j] = 1 + min(dp[i, j - 1], // Insert dp[i - 1, j], // Remove dp[i - 1, j - 1]); // Replace } } return dp[m, n]; } // Returns true if str1 and str2 are // k edit distance apart, else false. static bool areKDistant(string str1, string str2, int k) { int m = str1.Length; int n = str2.Length; if (Math.Abs(m - n) > k) return false; return (editDistDP(str1, str2, m, n) <= k); } // Driver Code public static void Main () { string str1 = "geek"; string str2 = "gks"; int k = 3; if(areKDistant(str1, str2, k)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by vt_m. |
PHP
<?php // A Dynamic Programming based // PHP program to find minimum // number operations is less // than or equal to k or not // to convert str1 to str2 // Utility function to find // minimum of three numbers function minn($x, $y, $z) { return min(minn($x, $y), $z); } function editDistDP($str1, $str2, $m, $n) { // Create a table to store // results of subproblems $dp[$m + 1][$n + 1] = 0; // Fill d[][] in bottom up manner for ($i = 0; $i <= $m; $i++) { for ($j = 0; $j <= $n; $j++) { // If first string is empty, // only option is to insert // all characters of second string if ($i == 0) // Min. operations = j $dp[$i][$j] = $j; // If second string is empty, // only option is to remove all // characters of second string else if ($j == 0) // Min. operations = i $dp[$i][$j] = $i; // If last characters are same, // ignore last char and recur // for remaining string else if ($str1[$i - 1] == $str2[$j - 1]) $dp[$i][$j] = $dp[$i - 1][$j - 1]; // If last character are different, // consider all possibilities and // find minimum else // Insert $dp[$i][$j] = 1 + min($dp[$i][$j - 1], // Remove $dp[$i - 1][$j], // Replace $dp[$i - 1][$j - 1]); } } return $dp[$m][$n]; } // Returns true if str1 and // str2 are k edit distance // apart, else false. function areKDistant($str1, $str2, $k) { $m = strlen($str1); $n = strlen($str2); if (abs($m - $n) > $k) return false; return (editDistDP($str1, $str2, $m, $n) <= $k); } // Driver Code $str1 = "geek"; $str2 = "gks"; $k = 3; if(areKDistant($str1, $str2, $k)) echo"Yes" ; elseecho "No"; // This code is contributed by nitin mittal. ?> |
Output :
Yes
This article is contributed by Sahil Chhabra (akku). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- A Program to check if strings are rotations of each other or not
- Check for balanced parentheses in an expression
- Check whether two strings are anagram of each other
- Print all interleavings of given two strings
- Interleaving of two given strings with no common characters
- Find if a string is interleaved of two other strings | DP-33
- Rearrange a string so that all same characters become d distance away
- Check if a given string is a rotation of a palindrome
- Find if an array of strings can be chained to form a circle | Set 1
- Given two strings, find if first string is a subsequence of second
- Check a given sentence for a given set of simple grammer rules
- Print all possible strings that can be made by placing spaces
- Find all distinct palindromic sub-strings of a given string
- Check if a given sequence of moves for a robot is circular or not
- Check if two given strings are isomorphic to each other

