Prefijo de longitud máxima de una string que aparece como subsecuencia en otra

Dadas dos strings s y t . La tarea es encontrar la longitud máxima de algún prefijo de la string S que aparece en la string t como subsecuencia.

Ejemplos: 

C++

// C++ program to find maximum 
// length prefix of one string 
// occur as subsequence in another
// string.
#include<bits/stdc++.h>
using namespace std;
  
// Return the maximum length 
// prefix which is subsequence.
int maxPrefix(char s[], char t[])
{
    int count = 0;
  
    // Iterating string T.
    for (int i = 0; i < strlen(t); i++)
    {
        // If end of string S.
        if (count == strlen(s))
            break;
  
        // If character match, 
        // increment counter.
        if (t[i] == s[count])
            count++;
    }
  
    return count;
}
  
// Driven Code
int main()
{
    char S[] = "digger";
    char T[] = "biggerdiagram";
  
    cout << maxPrefix(S, T) 
         << endl;
  
    return 0;
}

Java

// Java program to find maximum
// length prefix of one string 
// occur as subsequence in another
// string.
public class GFG {     
      
    // Return the maximum length 
    // prefix which is subsequence.
    static int maxPrefix(String s, 
                         String t)
    {
        int count = 0;
      
        // Iterating string T.
        for (int i = 0; i < t.length(); i++)
        {
            // If end of string S.
            if (count == s.length())
                break;
      
            // If character match,  
            // increment counter.
            if (t.charAt(i) == s.charAt(count))
                count++;
        }
      
        return count;
    }
      
    // Driver Code
    public static void main(String args[])
    {
        String S = "digger";
        String T = "biggerdiagram";
      
        System.out.println(maxPrefix(S, T));
    }
}
// This code is contributed by Sumit Ghosh

Python 3

# Python 3 program to find maximum 
# length prefix of one string occur
# as subsequence in another string.
  
  
# Return the maximum length 
# prefix which is subsequence.
def maxPrefix(s, t) :
    count = 0
  
    # Iterating string T.
    for i in range(0,len(t)) :
          
        # If end of string S.
        if (count == len(s)) :
            break
  
        # If character match, 
        # increment counter.
        if (t[i] == s[count]) :
            count = count + 1
              
  
    return count
  
  
# Driver Code
S = "digger"
T = "biggerdiagram"
  
print(maxPrefix(S, T))
  
  
# This code is contributed
# by Nikita Tiwari.

C#

// C# program to find maximum 
// length prefix of one string
// occur as subsequence in 
// another string.
using System;
  
class GFG 
{     
      
    // Return the maximum length prefix 
    // which is subsequence.
    static int maxPrefix(String s, 
                         String t)
    {
        int count = 0;
      
        // Iterating string T.
        for (int i = 0; i < t.Length; i++)
        {
            // If end of string S.
            if (count == s.Length)
                break;
      
            // If character match, 
            // increment counter.
            if (t[i] == s[count])
                count++;
        }
      
        return count;
    }
      
    // Driver Code
    public static void Main()
    {
        String S = "digger";
        String T = "biggerdiagram";
      
        Console.Write(maxPrefix(S, T));
    }
}
  
// This code is contributed by nitin mittal

PHP

<?php
// PHP program to find maximum 
// length prefix of one string 
// occur as subsequence in another
// string.
  
// Return the maximum length 
// prefix which is subsequence.
function maxPrefix($s, $t)
{
    $count = 0;
  
    // Iterating string T.
    for ($i = 0; $i < strlen($t); $i++)
    {
        // If end of string S.
        if ($count == strlen($s))
            break;
  
        // If character match,
        // increment counter.
        if ($t[$i] == $s[$count])
            $count++;
    }
  
    return $count;
}
  
// Driver Code
{
    $S = "digger";
    $T = "biggerdiagram";
  
    echo maxPrefix($S, $T) ;
  
    return 0;
}
  
// This code is contributed by nitin mittal.
?>

Javascript

<script>
  
// JavaScript program to find maximum
// length prefix of one string 
// occur as subsequence in another
// string.
  
    // Return the maximum length 
    // prefix which is subsequence.
    function maxPrefix(s,t)
    {
        let count = 0;
      
        // Iterating string T.
        for (let i = 0; i < t.length; i++)
        {
            // If end of string S.
            if (count == s.length)
                break;
      
            // If character match,  
            // increment counter.
            if (t[i] == s[count])
                count++;
        }
      
        return count;
    }
  
  
// Driver Code
  
    let S = "digger";
    let T = "biggerdiagram";
      
    document.write(maxPrefix(S, T)); 
          
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *