Longest Consecutively Increasing Substring - Facebook Top Interview Questions


Problem Statement :


You are given a string s containing lowercase alphabet characters as well as "?". 

For each "?" you must either delete it or replace it with any lowercase alphabet character.

Return the length of the longest consecutively increasing substring that starts with "a".

Constraints


1 ≤ n ≤ 100,000 where n is the length of s

Example 1

Input

s = "bca???de"

Output

5

Explanation

We can turn s into "bcabcde" and "abcde" is the longest consecutively increasing substring that starts 
with "a"



Example 2

Input

s = "zyx"

Output

0

Explanation

No substring here starts with "a"



Solution :



title-img




                        Solution in C++ :

int solve(string s) {
    int i, j, n = s.size(), ret = 0;
    vector<vector<int>> dp(n, vector<int>(26, 0));
    for (i = 0; i < n; i++) {
        if (s[i] == 'a')
            dp[i][0] = 1;
        else if (s[i] == '?') {
            dp[i][0] = 1;
            for (j = 1; i && j < 26; j++) {
                dp[i][j] = dp[i - 1][j];
                if (dp[i - 1][j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
            }
        } else {
            j = s[i] - 'a';
            if (i && dp[i - 1][j - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
        }
    }
    for (i = 0; i < n; i++)
        for (j = 0; j < 26; j++) ret = max(ret, dp[i][j]);
    return ret;
}
                    


                        Solution in Java :

import java.util.*;

class Solution {
    public int solve(String s) {
        dp = new Integer[s.length()][256];

        int res = 0;
        for (int i = 0; i < s.length(); i++)
            if (s.charAt(i) == 'a' || s.charAt(i) == '?')
                res = Math.max(res, lci(s, i, (char) ('a' - 1)));

        return res;
    }
    Integer[][] dp;
    private int lci(String s, int curr, char prev) {
        if (curr >= s.length() || (s.charAt(curr) != '?' && s.charAt(curr) != prev + 1)
            || prev == 'z')
            return 0;
        if (dp[curr][prev] != null)
            return dp[curr][prev];

        if (s.charAt(curr) == '?')
            return dp[curr][prev] =
                       Math.max(1 + lci(s, curr + 1, (char) (prev + 1)), lci(s, curr + 1, prev));
        return dp[curr][prev] = 1 + lci(s, curr + 1, s.charAt(curr));
    }
}
                    


                        Solution in Python : 
                            
class Solution:
    def solve(self, s):
        idx = 0
        ans = count = 0

        @cache
        def find(idx, exp):
            if ord(exp) == 123 or idx >= len(s):
                return 0
            if s[idx] == exp or s[idx] == "?":
                if s[idx] == "?":
                    return max(1 + find(idx + 1, chr(ord(exp) + 1)), find(idx + 1, exp))
                return 1 + find(idx + 1, chr(ord(exp) + 1))
            return 0

        while idx < len(s):
            if s[idx] == "a" or s[idx] == "?":
                ans = max(find(idx, "a"), ans)
            idx += 1
        return ans
                    


View More Similar Problems

Find Merge Point of Two Lists

This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share

View Solution →

Inserting a Node Into a Sorted Doubly Linked List

Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function

View Solution →

Reverse a doubly linked list

This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.

View Solution →

Tree: Preorder Traversal

Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's

View Solution →

Tree: Postorder Traversal

Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the

View Solution →

Tree: Inorder Traversal

In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func

View Solution →