Zipped String - Amazon Top Interview Questions

Problem Statement :

Given lowercase alphabet strings a, b, and c return whether there's any way of obtaining c by merging characters in order from a and b.


0 ≤ n ≤ 1,000 where n is the length of a
0 ≤ m ≤ 1,000 where m is the length of b
0 ≤ k ≤ 1,000 where k is the length of c

Example 1


a = "abc"
b = "def"
c = "abdefc"




abdefc is an interleave since abc and def can be interleaved with: abdefc

Example 2


a = "ab"
b = "cd"
c = "abdc"




Even though ab is in the right order in abdc, cd is backwards, so this is not an interleaving.

Solution :


                        Solution in C++ :

bool solve(string a, string b, string c) {
    if (a.size() + b.size() != c.size()) return false;
    vector<vector<bool>> dp(a.size() + 1, vector<bool>(b.size() + 1, false));
    dp[0][0] = true;
    for (int i = 0; i <= a.size(); i++) {
        for (int j = 0; j <= b.size(); j++) {
            if (j > 0) {
                dp[i][j] = dp[i][j - 1] && b[j - 1] == c[i + j - 1];
            if (i > 0) {
                dp[i][j] = dp[i][j] || (dp[i - 1][j] && a[i - 1] == c[i + j - 1]);
    return dp[a.size()][b.size()];

                        Solution in Java :

import java.util.*;

class Solution {
    public boolean solve(String a, String b, String c) {
        if (a.length() + b.length() != c.length())
            return false;

        int indexA = 0, indexB = 0, prevA = -1, prevB = -1, prevIndex = -1;
        boolean goBack = false;

        for (int i = 0; i < c.length(); i++) {
            if (indexA < a.length() && a.charAt(indexA) == c.charAt(i)) {
                if (!goBack && indexB < b.length() && b.charAt(indexB) == c.charAt(i)) {
                    prevA = indexA;
                    prevB = indexB;
                    prevIndex = i;
                    goBack = true;
            } else if (indexB < b.length() && b.charAt(indexB) == c.charAt(i))
            else if (goBack) {
                indexA = prevA;
                indexB = prevB + 1;
                i = prevIndex;

                goBack = false;
            } else
                return false;

        return true;

                        Solution in Python : 
class Solution:
    def solve(self, a, b, c):
        if len(a) + len(b) != len(c):
            return False

        def f(a_idx, b_idx, c_idx):
            if c_idx == len(c):
                return True
            possible = False
            if a_idx < len(a) and c[c_idx] == a[a_idx]:
                possible = possible or f(a_idx + 1, b_idx, c_idx + 1)
            if b_idx < len(b) and c[c_idx] == b[b_idx]:
                possible = possible or f(a_idx, b_idx + 1, c_idx + 1)
            return possible

        return f(0, 0, 0)

View More Similar Problems


We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →