Find the longest common prefix between two strings after performing swaps on second string

Given two strings a and b. Find the longest common prefix between them after performing zero or more operation on string b. In each operation, you can swap any two letters.

Examples:

Input : a = "here", b = "there"
Output : 4
The 2nd string can be made "heret" by just 
swapping characters and thus the longest
prefix is of length 4.

Input : a = "you", b = "me"
Output : 0

Given that we are only allowed to performs swaps in the string b and the length of prefix should be maximized. So the idea is to traverse string a and check if the frequency of current character in string a is same or less of that in string b. If yes then move forward in string a otherwise break and print the length of the part of string a up to which a character is matched in string b.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the longest
// common prefix between two strings 
// after performing swaps on the second string
#include <bits/stdc++.h>
using namespace std;
  
   
void LengthLCP(string x, string y)
{
      
  
    int fr[26]={0};
       
    int a = x.length(); // length of x
    int b = y.length(); // length of y
       
    for (int i=0 ;i<b ; i++)
    {
        // creating frequency array of
        // characters of y
        fr[y[i] - 97] += 1;
    }
    // storing the length of 
    // longest common prefix
    int c = 0;
       
    for (int i=0 ;i<a ; i++)
    {
        // checking if the frequency of the character at
        // position i in x in b is greater than zero or not
        // if zero we increase the prefix count by 1
        if (fr[x[i] - 97] > 0){
            c += 1;
            fr[x[i] - 97] -= 1;
        }
        else
            break;
    }
    cout<<(c)<<endl;
}
// Driver Code
int main()
{
string x="here", y =  "there";
   
LengthLCP(x, y);
  
return 0;
}
//contributed by Arnab Kundu

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the longest
// common prefix between two strings 
// after performing swaps on the second string
  
public class GFG {
  
    static void LengthLCP(String x, String y)
    {
          
  
        int fr[]=new int [26];
           
        int a = x.length(); // length of x
        int b = y.length(); // length of y
           
        for (int i=0 ;i<b ; i++)
        {
            // creating frequency array of
            // characters of y
            fr[y.charAt(i) - 97] += 1;
        }
        // storing the length of 
        // longest common prefix
        int c = 0;
           
        for (int i=0 ;i<a ; i++)
        {
            // checking if the frequency of the character at
            // position i in x in b is greater than zero or not
            // if zero we increase the prefix count by 1
            if (fr[x.charAt(i) - 97] > 0){
                c += 1;
                fr[x.charAt(i) - 97] -= 1;
            }
            else
                break;
        }
        System.out.println((c)) ;
    }
  
  
    public static void main(String args[])
    {
        String x="here", y =  "there";
           
        LengthLCP(x, y);
  
  
    }
    // This code is contributed by ANKITRAI1
}
   

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to find the longest
# common prefix between two strings 
# after performing swaps on the second string
  
def LengthLCP(x, y):
    fr = [0] * 26
      
    a = len(x) # length of x
    b = len(y) # length of y
      
    for i in range(b):
        # creating frequency array of
        # characters of y
        fr[ord(y[i]) - 97] += 1
      
    # storing the length of 
    # longest common prefix
    c = 0 
      
    for i in range(a):
        # checking if the frequency of the character at
        # position i in x in b is greater than zero or not
        # if zero we increase the prefix count by 1
        if (fr[ord(x[i]) - 97] > 0):
            c += 1
            fr[ord(x[i]) - 97] -= 1
        else:
            break
    print(c)
  
# Driver Code
  
x, y = "here", "there"
  
LengthLCP(x, y)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the longest 
// common prefix between two strings 
// after performing swaps on the 
// second string
using System;
  
class GFG 
  
static void LengthLCP(String x, String y) 
    int []fr = new int [26]; 
      
    int a = x.Length; // length of x 
    int b = y.Length; // length of y 
      
    for (int i = 0 ; i < b; i++) 
    
        // creating frequency array 
        // of characters of y 
        fr[y[i] - 97] += 1; 
    
      
    // storing the length of 
    // longest common prefix 
    int c = 0; 
      
    for (int i = 0 ; i < a; i++) 
    
        // checking if the frequency of 
        // the character at position i
        // in x in b is greater than zero 
        // or not if zero we increase the 
        // prefix count by 1 
        if (fr[x[i] - 97] > 0)
        
            c += 1; 
            fr[x[i] - 97] -= 1; 
        
        else
            break
    
    Console.Write((c)) ; 
  
// Driver Code
public static void Main() 
    String x = "here", y = "there"
      
    LengthLCP(x, y); 
  
// This code is contributed by 29AjayKumar

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php 
// PHP program to find the longest
// common prefix between two strings 
// after performing swaps on the second string
  
function LengthLCP($x, $y)
{
      
  
    $fr = array_fill(0,26,NULL);
      
    $a = strlen($x); // length of x
    $b = strlen($y); // length of y
      
    for ($i = 0 ;$i < $b ; $i++)
    {
        // creating frequency array of
        // characters of y
        $fr[ord($y[$i]) - 97] += 1;
    }
      
    // storing the length of 
    // longest common prefix
    $c = 0;
      
    for ($i = 0 ;$i < $a ; $i++)
    {
        // checking if the frequency of the character at
        // position i in x in b is greater than zero or not
        // if zero we increase the prefix count by 1
        if ($fr[ord($x[$i]) - 97] > 0)
        {
            $c += 1;
            $fr[ord($x[$i]) - 97] -= 1;
        }
        else
            break;
    }
    echo $c;
}
  
// Driver Code
$x="here";
$y = "there";
  
LengthLCP($x, $y);
  
return 0;
  
// This code is contributed by ChitraNayal
?>

chevron_right


Output:

4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.