One Edit Distance - Amazon Top Interview Questions
Problem Statement :
Given two strings s0 and s1 determine whether they are one or zero edit distance away. An edit can be described as deleting a character, adding a character, or replacing a character with another character. Constraints n ≤ 100,000 where n is the length of s0. m ≤ 100,000 where m is the length of s1. Example 1 Input s0 = "quicksort" s1 = "quicksort" Output True Explanation This has the edit distance of 0, since they are the same string. Example 2 Input s0 = "mergesort" s1 = "mergesorts" Output True Explanation This has the edit distance 1, since s was added to the second string. Example 3 Input s0 = "mergeport" s1 = "mergesorts" Output False Explanation This has edit distance of 2.
Solution :
Solution in C++ :
bool equal(string& s0, int i, string& s1, int j) {
while (i < s0.size() && j < s1.size() && s0[i] == s1[j]) {
i++, j++;
}
return i == s0.size() && j == s1.size();
}
bool solve(string s0, string s1) {
int m = s0.size(), n = s1.size();
if (m > 1 + n || n > 1 + m) return false;
for (int i = 0; i < min(m, n); i++) {
if (s0[i] != s1[i])
return equal(s0, i + 1, s1, i) || equal(s0, i, s1, i + 1) ||
equal(s0, i + 1, s1, i + 1);
}
return true;
}
Solution in Java :
class Solution {
public boolean solve(String s0, String s1) {
int m = s0.length(), n = s1.length();
if (Math.abs(m - n) > 1) {
return false;
}
int i = 0, j = 0, edit = 0;
while (i < m && j < n) {
if (s0.charAt(i) != s1.charAt(j)) {
edit += 1;
if (m < n) {
i--;
} else if (m > n) {
j--;
}
}
i++;
j++;
}
return (edit < 2);
}
}
Solution in Python :
class Solution:
def solve(self, s0, s1):
# Get dimensions
n = len(s0)
m = len(s1)
# Ensure n < m
if m < n:
return self.solve(s1, s0)
# Check if length diff is > 1
if abs(n - m) > 1:
return False
# index i -> s0, index j -> s1
i = 0
j = 0
edits = 0
while i < n:
# chars match, increment i, j :D
if s0[i] == s1[j]:
i += 1
j += 1
# mismatch, count edit >:(
else:
edits += 1
# same length, replace char in s0, move i, j
if n == m:
i += 1
j += 1
# not same length, delete char from s1 by moving j
else:
j += 1
# too many edits, terminate
if edits > 1:
break
# 1 edit or less?
return edits <= 1
View More Similar Problems
Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →Balanced Forest
Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a
View Solution →Jenny's Subtrees
Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .
View Solution →