Given two strings and . Find the longest common prefix between them after performing zero or more operation on string . In each operation, you can swap any two letters.
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 otherwise break and print the length of the part of string a up to which a character is matched in string .
Below is the implementation of the above approach:
- Longest Common Prefix using Trie
- Longest Common Prefix using Sorting
- Longest Common Prefix Matching | Set-6
- Longest Common Prefix using Binary Search
- Longest Common Prefix using Linked List
- Longest Common Prefix using Divide and Conquer Algorithm
- Python | Ways to determine common prefix in set of strings
- Longest common anagram subsequence from N strings
- Longest string in an array which matches with prefix of the given string
- Print the longest prefix of the given string which is also the suffix of the same string
- Longest Common Prefix using Word by Word Matching
- Longest Common Prefix using Character by Character Matching
- Longest prefix in a string with highest frequency
- Count of strings whose prefix match with the given string to a given length k
- Sub-strings of a string that are prefix of the same string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.