Change language

# 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 [26] = {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 ` ` [26]; `   ` 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 ` `); `

``` ```

``` (adsbygoogle = window.adsbygoogle || []).push({}); Shop Learn programming in R: courses\$ Best Python online courses for 2022\$ 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\$ (adsbygoogle = window.adsbygoogle || []).push({}); Latest questions NUMPYNUMPY Common xlabel/ylabel for matplotlib subplots 12 answers NUMPYNUMPY How to specify multiple return types using type-hints 12 answers NUMPYNUMPY Why do I get "Pickle - EOFError: Ran out of input" reading an empty file? 12 answers NUMPYNUMPY Flake8: Ignore specific warning for entire file 12 answers NUMPYNUMPY glob exclude pattern 12 answers NUMPYNUMPY How to avoid HTTP error 429 (Too Many Requests) python 12 answers NUMPYNUMPY Python CSV error: line contains NULL byte 12 answers NUMPYNUMPY csv.Error: iterator should return strings, not bytes 12 answers All questions (adsbygoogle = window.adsbygoogle || []).push({}); ```
``` ```
``` ```
``` News 27/11/2022 The long-awaited ARPlAN app update for Android is out 23/11/2022 Apple and Epic Systems to develop macOS health apps 21/11/2022 Iranian hackers hacked into US federal agency using old vulnerability Wiki File handling Python | How to copy data from one Excel sheet to another __dict__ Common xlabel/ylabel for matplotlib subplots __dict__ Check if one list is a subset of another in Python sin How to specify multiple return types using type-hints exp Printing words vertically in Python exp Python Extract words from a given string Python functions Cyclic redundancy check in Python Counters Finding mean, median, mode in Python without libraries cos Python add suffix / add prefix to strings in a list __dict__ Why do I get "Pickle - EOFError: Ran out of input" reading an empty file? Python functions Python - Move item to the end of the list Loops Python - Print list vertically © 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'); ```