Find the longest common prefix between two strings after performing swaps on second string
Last Updated :
25 Apr, 2023
Given two strings and . Find the longest common prefix between them after performing zero or more operations on string . 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 and the length of prefix should be maximized. So the idea is to traverse string and check if the frequency of current character in string is same or less of that in string . If yes then move forward in string an otherwise break and print the length of the part of string an up to which a character is matched in string .
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void LengthLCP(string x, string y)
{
int fr[26]={0};
int a = x.length();
int b = y.length();
for ( int i=0 ;i<b ; i++)
{
fr[y[i] - 97] += 1;
}
int c = 0;
for ( int i=0 ;i<a ; i++)
{
if (fr[x[i] - 97] > 0){
c += 1;
fr[x[i] - 97] -= 1;
}
else
break ;
}
cout<<(c)<<endl;
}
int main()
{
string x= "here" , y = "there" ;
LengthLCP(x, y);
return 0;
}
|
Java
public class GFG {
static void LengthLCP(String x, String y)
{
int fr[]= new int [ 26 ];
int a = x.length();
int b = y.length();
for ( int i= 0 ;i<b ; i++)
{
fr[y.charAt(i) - 97 ] += 1 ;
}
int c = 0 ;
for ( int i= 0 ;i<a ; i++)
{
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);
}
}
|
Python3
def LengthLCP(x, y):
fr = [ 0 ] * 26
a = len (x)
b = len (y)
for i in range (b):
fr[ ord (y[i]) - 97 ] + = 1
c = 0
for i in range (a):
if (fr[ ord (x[i]) - 97 ] > 0 ):
c + = 1
fr[ ord (x[i]) - 97 ] - = 1
else :
break
print (c)
x, y = "here" , "there"
LengthLCP(x, y)
|
C#
using System;
class GFG
{
static void LengthLCP(String x, String y)
{
int []fr = new int [26];
int a = x.Length;
int b = y.Length;
for ( int i = 0 ; i < b; i++)
{
fr[y[i] - 97] += 1;
}
int c = 0;
for ( int i = 0 ; i < a; i++)
{
if (fr[x[i] - 97] > 0)
{
c += 1;
fr[x[i] - 97] -= 1;
}
else
break ;
}
Console.Write((c)) ;
}
public static void Main()
{
String x = "here" , y = "there" ;
LengthLCP(x, y);
}
}
|
PHP
<?php
function LengthLCP( $x , $y )
{
$fr = array_fill (0,26,NULL);
$a = strlen ( $x );
$b = strlen ( $y );
for ( $i = 0 ; $i < $b ; $i ++)
{
$fr [ord( $y [ $i ]) - 97] += 1;
}
$c = 0;
for ( $i = 0 ; $i < $a ; $i ++)
{
if ( $fr [ord( $x [ $i ]) - 97] > 0)
{
$c += 1;
$fr [ord( $x [ $i ]) - 97] -= 1;
}
else
break ;
}
echo $c ;
}
$x = "here" ;
$y = "there" ;
LengthLCP( $x , $y );
return 0;
?>
|
Javascript
<script>
function LengthLCP(x, y)
{
let fr = Array(26).fill(0);
let a = x.length;
let b = y.length;
for (let i=0 ;i<b ; i++)
{
fr[y[i].charCodeAt() - 97] += 1;
}
let c = 0;
for (let i=0 ;i<a ; i++)
{
if (fr[x[i].charCodeAt() - 97] > 0){
c += 1;
fr[x[i].charCodeAt() - 97] -= 1;
}
else
break ;
}
document.write((c)) ;
}
let x= "here" , y = "there" ;
LengthLCP(x, y);
</script>
|
Time Complexity: O(length(x) + length(y))
Space Complexity: O(26)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...