Skip to main content

Longest String Chain | Leetcode 1048 solution

 You are given an array of words where each word consists of lowercase English letters.

wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB.

  • For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".

word chain is a sequence of words [word1, word2, ..., wordk] with k >= 1, where word1 is a predecessor of word2word2 is a predecessor of word3, and so on. A single word is trivially a word chain with k == 1.

Return the length of the longest possible word chain with words chosen from the given list of words.

 

Example 1:

Input: words = ["a","b","ba","bca","bda","bdca"]
Output: 4
Explanation: One of the longest word chains is ["a","ba","bda","bdca"].

Example 2:

Input: words = ["xbc","pcxbcf","xb","cxbc","pcxbc"]
Output: 5
Explanation: All the words can be put in a word chain ["xb", "xbc", "cxbc", "pcxbc", "pcxbcf"].

Example 3:

Input: words = ["abcd","dbqca"]
Output: 1
Explanation: The trivial word chain ["abcd"] is one of the longest word chains.
["abcd","dbqca"] is not a valid word chain because the ordering of the letters is changed.

 

Constraints:

  • 1 <= words.length <= 1000
  • 1 <= words[i].length <= 16
  • words[i] only consists of lowercase English letters.


solution : 


class Solution {
public:
  static bool solve(string &a , string &b)
  {
      return a.length() < b.length();
  }
    bool compare(string &a , string &b )
    {
        if(a.length() < b.length())
        {
            return compare(b , a);
        }
        if(a.length() != b.length() +1)
        {
            return false;
        }
        int flag =0;
        int j =0;
        int size = a.size();
        for(int i =0 ; i < size ;i++)
        {
            if(a[i] != b[j])
            {   
                if(flag ==1)
                {
                    return false;
                }
                flag =1;
                continue;
            }
            else
            {
                j++;
            }

        }
        if( j == b.length())
        {
            return true;
        }
        return false;
    }
    int longestStrChain(vector<string>& words) {
       
          int cnt = INT_MIN;
          sort(words.begin() , words.end(), solve);
          int size = words.size();
          vector<int>dp(size +1 , 1);
          for(int i = 0; i < size ; i++)
          {
              for(int j = 0 ; j < i ; j++)
              {
                  if(compare(words[i], words[j]) )
                  {
                      dp[i] = max(dp[i], dp[j] +1);
                  }
              }
              cnt = max(cnt , dp[i]);
          }
          return cnt;
    }
};

Comments

Popular posts from this blog

[PDF DOWNLOAD] AKTU Quantum series data structure b.tech 2nd year download

  All AKTU Quantums are available here. Get your hands on AKTU Quantums and boost your grades in AKTU semester exams. You can either search them category wise or can use the search bar or can manually search on this page. Download aktu second year quantum pdf data structures  download  data structures quantum aktu download aktu data structures quantum click here to download  write in comment section if you want quantum of any other subject.

solution : Snape and Ladder | codechef | Problem Code: SNAPE

  Snape and Ladder  Problem Code:  SNAPE Professor Snape has lots of potions. Bottles containing all types of potions are stacked on shelves which cover the entire wall from floor to ceiling. Professor Snape has broken his bones several times while climbing the top shelf for retrieving a potion. He decided to get a ladder for him. But he has no time to visit Diagon Alley. So he instructed Ron Weasley to make a ladder for him. Professor Snape specifically wants a step ladder which looks like an inverted 'V' from side view. Professor just mentioned two things before vanishing- B  - separation between left side (LS) and right side (RS) on the ground LS  - the length of left side What should be the length of  RS ? At one extreme  LS  can be vertical and at other  RS  can be vertical. Ron is angry and confused. Since Harry is busy battling Voldemort, its your duty to help him find the minimum and maximum length of  RS . Input First line contains single integer  T , the number of tes

solution: Count Total Set Bits | interviewbit | Amazon previous year problems

  Problem Description Given a positive integer A , the task is to count the total number of set bits in the binary representation of all the numbers from 1 to A . Return the count modulo 10 9 + 7 . Problem Constraints 1 <= A <= 10 9 Input Format First and only argument is an integer A . Output Format Return an integer denoting the ( Total number of set bits in the binary representation of all the numbers from 1 to A )modulo 10 9 + 7 . Example Input Input 1: A = 3 Input 2: A = 1 Example Output Output 1: 4 Output 2: 1 Example Explanation Explanation 1: DECIMAL BINARY SET BIT COUNT 1 01 1 2 10 1 3 11 2 1 + 1 + 2 = 4 Answer = 4 % 1000000007 = 4 Explanation 2: A = 1 DECIMAL BINARY SET BIT COUNT 1 01 1 Answer = 1 % 1000000007 = 1 solution : int  Solution::solve ( int  A )   {      if ( A  ==   0 )      {          return   0 ;      }      if ( A  == 1    )      {          return   1