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
Poisonous Plants
There are a number of plants in a garden. Each of the plants has been treated with some amount of pesticide. After each day, if any plant has more pesticide than the plant on its left, being weaker than the left one, it dies. You are given the initial values of the pesticide in each of the plants. Determine the number of days after which no plant dies, i.e. the time after which there is no plan
View Solution →AND xor OR
Given an array of distinct elements. Let and be the smallest and the next smallest element in the interval where . . where , are the bitwise operators , and respectively. Your task is to find the maximum possible value of . Input Format First line contains integer N. Second line contains N integers, representing elements of the array A[] . Output Format Print the value
View Solution →Waiter
You are a waiter at a party. There is a pile of numbered plates. Create an empty answers array. At each iteration, i, remove each plate from the top of the stack in order. Determine if the number on the plate is evenly divisible ith the prime number. If it is, stack it in pile Bi. Otherwise, stack it in stack Ai. Store the values Bi in from top to bottom in answers. In the next iteration, do the
View Solution →Queue using Two Stacks
A queue is an abstract data type that maintains the order in which elements were added to it, allowing the oldest elements to be removed from the front and new elements to be added to the rear. This is called a First-In-First-Out (FIFO) data structure because the first element added to the queue (i.e., the one that has been waiting the longest) is always the first one to be removed. A basic que
View Solution →Castle on the Grid
You are given a square grid with some cells open (.) and some blocked (X). Your playing piece can move along any row or column until it reaches the edge of the grid or a blocked cell. Given a grid, a start and a goal, determine the minmum number of moves to get to the goal. Function Description Complete the minimumMoves function in the editor. minimumMoves has the following parameter(s):
View Solution →Down to Zero II
You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations N on in each move: 1: If we take 2 integers a and b where , N = a * b , then we can change N = max( a, b ) 2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0. Input Format The first line contains the integer Q.
View Solution →