
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to find longest prefix that is also a suffix in C++
Suppose we have a string s, we have to find the longest prefix of s, which is also a suffix (excluding itself). If there is no such prefix, then simply return blank string.
So, if the input is like "madam", then the output will be "m", it has 4 prefixes excluding itself. These are "m", "ma", "mad", "mada" and 4 suffixes like "m", "am", "dam", "adam". The largest prefix which is also suffix is given by "m".
To solve this, we will follow these steps −
Define a function lps(), this will take s,
n := size of s
Define an array ret of size n
j := 0, i := 1
while i < n, do −
if s[i] is same as s[j], then −
ret[i] := j + 1
(increase i by 1)
(increase j by 1)
otherwise when s[i] is not equal to s[j], then −
if j > 0, then −
j := ret[j − 1]
Otherwise
(increase i by 1)
return ret
From the main method do the following −
n := size of s
if n is same as 1, then −
return blank string
Define an array v = lps(s)
x := v[n − 1]
ret := blank string
for initialize i := 0, when i < x, update (increase i by 1), do −
ret := ret + s[i]
return ret
Let us see the following implementation to get better understanding −
Example
#include <bits/stdc++.h> using namespace std; class Solution { public: vector <int> lps(string s){ int n = s.size(); vector<int> ret(n); int j = 0; int i = 1; while (i < n) { if (s[i] == s[j]) { ret[i] = j + 1; i++; j++; } else if (s[i] != s[j]) { if (j > 0) j = ret[j - 1]; else { i++; } } } return ret; } string longestPrefix(string s) { int n = s.size(); if (n == 1) return ""; vector<int> v = lps(s); int x = v[n - 1]; string ret = ""; for (int i = 0; i < x; i++) { ret += s[i]; } return ret; } }; main(){ Solution ob; cout << (ob.longestPrefix("helloworldhello")); }
Input
"helloworldhello"
Output
hello
- Related Articles
- Find the longest sub-string which is prefix, suffix and also present inside the string in Python
- Print the longest prefix of the given string which is also the suffix of the same string in C Program.
- Program to find length longest prefix sequence of a word array in Python
- C++ Program to Find the Longest Prefix Matching of a Given Sequence
- Program to find longest common prefix from list of strings in Python
- match_results prefix() and suffix() in C++
- Program to find number of ways we can arrange letters such that each prefix and suffix have more Bs than As in Python
- Longest Common Prefix in Python
- Longest Happy Prefix in C++
- How to Add Prefix or Suffix to a Range of Cells in Excel
- Find minimum shift for longest common prefix in C++
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in Python
- Find index i such that prefix of S1 and suffix of S2 till i form a palindrome when concatenated in C++
- Check if suffix and prefix of a string are palindromes in Python
- How to Add Prefix or Suffix into Cell Values in Google Sheets?
