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

| |

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 `

Considering that we are only allowed to perform permutations in the line and the prefix length should be maximum. So the idea is to go through the line and check if the frequency of the current character in the line the same or less than the line , If yes, then move forward to line otherwise break and print the length of the part of line a to which the character in line ,

Below is the implementation of the above approach:

C++

 ` // C++ program for finding the longest ` ` // common prefix between two lines ` ` // after swapping on the second line ` ` # include "bits / stdC++. h" ` ` using ` ` namespace ` ` std; `     ` void ` ` LengthLCP (string x, string y) ` ` {`     ` int ` ` fr  = {0}; `   ` int ` ` a = x.length (); ` ` // length х ` ` int ` ` b = y.length (); ` ` // length for `   ` for ` ` (` ` int ` ` i = 0; i "b; i ++) ` ` {` ` // create a frequency array ` ` // characters at ` ` fr [y [i] - 97] + = 1; ` `} ` ` // keep the length ` ` ` ` // longest common prefix ` ` int ` ` c = 0; `   ` for ` ` (` ` int ` ` i = 0; i "a ; i ++) ` ` {` ` // check character frequency for ` ` // position i in x in b is greater than zero or not ` ` // if zero, we increase the number of prefixes 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; ` `} ` ` // provided by Arnab Kundu `

Java

 ` // Java program for finding the longest ` ` // common prefix between the two lines ` ` // after swapping on the second line `   ` public ` ` class ` ` GFG {`   ` static ` ` void ` ` LengthLCP (String x, String y) ` ` {`     ` int ` ` fr [] = ` ` new ` ` int ` ` [` ` 26 ` `]; `   ` int ` ` a = x.length (); ` ` // length x ` ` int ` ` b = y.length (); ` ` // length for `   ` for ` ` (` ` int ` ` i = ` ` 0 ` `; i "b; i ++) ` ` {` ` // create a frequency array ` ` // symbols for ` ` fr [y.charAt (i) - ` ` 97 ` `] + = ` ` 1 ` `; ` `} ` ` // keep the length ` ` ` ` // longest common prefix ` ` int ` ` c = ` ` 0 ` `; `   ` for ` ` (` ` int ` ` i = ` ` 0 ` `; i "a; i ++) ` ` {` ` // check character frequency for ` ` // position i in x in b is greater than zero or not ` ` // if zero, we increase the number of prefixes 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 provided by ANKITRAI1 ` `} ` ` `

python3

` `

` # Python program to find the longest # common prefix between two lines # after swapping on the second line   def LengthLCP (x, y): fr = [ 0 ] * 26   a = len (x) # length x b = len (y) # length at     for i in range (b): # create a frequency array   # of y characters   fr [ ord (y [i]) - 97 ] + = 1     # length storage # the most long common prefix c = 0     for i in range ( a): # check character frequency for # position i in x in b is greater than zero or not # if zero, we increase the number of prefixes 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) `

` ` C #

PHP

 ` // C # program for finding the longest ` ` // common prefix between two lines ` ` // after swaps to ` ` // second line ` ` using ` ` System; `   ` class ` ` GFG ` ` { ` ` `  ` static ` ` void ` ` LengthLCP (String x, String y) ` ` { ` ` int ` ` [] fr = ` ` new ` ` int ` ` ; `   ` int ` ` a = x.Length; ` ` // length x ` ` int ` ` b = y.Length; ` ` // length for `   ` for ` ` (` ` int ` ` i = 0; i "b; i ++) ` ` { ` ` // create a frequency array ` ` // y characters ` ` fr [y [i] - 97] + = 1; ` `} `   ` ` ` // keep the length ` ` // longest common prefix ` ` ` ` int ` ` c = 0; `   ` for ` ` (` ` int ` ` i = 0; i "a; i ++) ` ` { ` ` // check frequency ` ` // character at position i ` ` ` ` // xcb greater than zero ` ` // or not, if zero, we increase ` ` // number of prefixes 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 provided by 29AjayKumar `
 ` & lt;? Php ` ` / / PHP program to find the longest ` ` // common prefix between two lines ` ` // after execution permutations on the second line `   ` function ` ` LengthLCP (` ` \$ x ` `, ` ` \$ y ` `) ` ` {`   ` `  ` \$ fr ` ` = ` ` array_fill ` ` (0,26, NULL); `   ` \$ a ` ` = ` ` strlen ` ` (` ` \$ x ` `); ` ` // length x ` ` \$ b ` ` = ` ` strlen ` ` (` ` \$ y ` `); ` ` // length for `   ` for ` ` (` ` \$ i ` ` = 0; ` ` \$ i ` ` "` ` \$ b ` `; ` ` \$ i ` ` ++) ` ` {` ` // create a frequency array ` ` // symbols y ` ` \$ fr ` ` [ord ( ` ` \$ y ` ` [` ` \$ i ` `]) - 97] + = 1; ` `} `   ` ` ` // keep the length ` ` // longest common prefix ` ` ` ` \$ c ` ` = 0; `   ` for ` ` (` ` \$ i ` ` = 0; ` ` \$ i ` ` "` ` \$ a ` `; ` ` \$ i ` ` ++) ` ` {` ` // check the character frequency for ` ` // position i in x in b is greater than zero or not ` ` // if zero, we increase the number of prefixes 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 provided by ChitraNayal ` `?" `

Exit:

` 4 `
}

` else `

` ` ` break ` `; `

`} `

` echo ` ` \$ c ` `; `

`} `

` // Driver code `

` \$ x ` ` = ` ` "here" ` `; `

` \$ y ` ` = ` ` "there" ` `; `

` LengthLCP (` ` \$ x ` `, ` ` \$ y ` `); `

` return ` ` 0; `

` // This code is provided by ChitraNayal `
`?" `

Exit:

` 4 `
}

` else `

` ` ` break ` `; `

`} `

` echo ` ` \$ c ` `; `

`} `

` // Driver code `

` \$ x ` ` = ` ` "here" ` `; `

` \$ y ` ` = ` ` "there" ` `; `

` LengthLCP (` ` \$ x ` `, ` ` \$ y ` `); `

```Find the longest common prefix between two strings after performing permutations on the second string find: Questions ```

## `Find the longest common prefix between two strings after performing permutations on the second string Python functions: Questions`

``` ```
``` ```
``` (adsbygoogle = window.adsbygoogle || []).push({}); Shop Best laptop for Fortnite\$ Best laptop for Excel\$ Best laptop for Solidworks\$ Best laptop for Roblox\$ Best computer for crypto mining\$ Best laptop for Sims 4\$ Best laptop for Zoom\$499 Best laptop for Minecraft\$590 Latest questions NUMPYNUMPY psycopg2: insert multiple rows with one query 12 answers NUMPYNUMPY How to convert Nonetype to int or string? 12 answers NUMPYNUMPY How to specify multiple return types using type-hints 12 answers NUMPYNUMPY Javascript Error: IPython is not defined in JupyterLab 12 answers All questions ```
``` ```
``` ```
``` News 29/06/2022 New Chinese processor for servers from Zhaoxin KH-40000: will it catch up and overtake AMD and Intel? 29/06/2022 Apple patents Pro Display XDR dual monitor stand 29/06/2022 The craftsman has released a new driver for 3dfx Voodoo 4/5 to work on modern widescreen monitors Wiki __del__ Python OpenCV | cv2.putText () method __del__ numpy.arctan2 () in Python __del__ Python | os.path.realpath () method around Python OpenCV | cv2.circle () method cvtcolor Python OpenCV cv2.cvtColor () method Python functions Python - Move item to the end of the list Counters time.perf_counter () function in Python __dict__ Check if one list is a subset of another in Python __del__ Python os.path.join () method © 2017—2022 Python Engineering Hub EN | ES | DE | FR | IT | RU | TR | PL | PT | JP | KR | CN | HI | NL Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Python Loops Counters NumPy NLP Regular Expressions Wiki Tech news Python Wiki StackOverflow PHP JavaScript Books All books Computations Development Cryptography For dummies Big Data document.addEventListener("DOMContentLoaded", () => { let arrayCode = document.querySelectorAll('pre'); arrayCode.forEach(element => { element.classList.add("prettyprint"); }); }); window.dataLayer = window.dataLayer || []; function gtag(){dataLayer.push(arguments);} gtag('js', new Date()); gtag('config', 'G-Q022WLXW4X'); // if you want to use the 'fire' or 'disable' fn, // you need to save OuiBounce to an object var _ouibounce = ouibounce(document.getElementById('ouibounce-modal'), { aggressive: true, timer: 0, delay: 1000, callback: function() { console.log('ouibounce fired!'); } }); \$('body').on('click', function() { \$('#ouibounce-modal').hide(); }); \$('#ouibounce-modal .modal-footer').on('click', function() { \$('#ouibounce-modal').hide(); }); \$('#ouibounce-modal .modal').on('click', function(e) { e.stopPropagation();}); \$("#reload_form_button" ).click(function() { console.log("hui"); \$('#reload_form').load('/wp-content/themes/nimani/af.html'); }); ```