


C++ program to find if a given string has a repeating subsequence of length 2 or more
Given a string, find a subsequence of length at least two that is repeated in the string. The indices of the subsequence element numbers cannot be in the same order.
1 2 |
|
Let’s look at a few examples below to understand how this approach works in different situations -
Example 1 - str = "PNDPNSP"
Explanation − Here, the answer is true because there is a recurring subsequence "PN" in the string.
Example 2 - str = "PPND"
Explanation - Here, the answer is wrong because there is no repeated subsequence of length at least two in the string.
Example 3 − str = "PPNP"
Explanation - Here, the answer is correct because "PP" indexes 0 and 1 and "PP" indexes 1 and 3 exist, and the "PP" used have different orderings in the subsequence index of. (0-based index)
The Chinese translation ofBrute force Approach
is:Brute force approach
This method will generate all possible subsequences of length 2 (minimum length) and find if we have already seen that subsequence with the found subsequence. If the subsequence already exists, we return true and terminate the program; otherwise, after completing the iteration, if we found nothing, we return false.
In the worst case, this subsequence may not exist and we end up generating all possible results.
subsequences of two lengths and store them. So assuming you hash the calculated subsequence to achieve O(1) insertion and search, this becomes O(n^2). The total subsequence is also O(n^2), so the storage space is as well.Modified longest common subsequence (LCS)
LCS algorithm finds the longest common subsequence in 2 strings. It is a standard dynamic programming method that uses an iterative method of two-dimensional matrices. The time complexity is O(n^2). We will only search the given string itself in the modified method. Nonetheless, we will also check if the index of the current position is not the same.
Example
See the C code below to implement the modified longest common subsequence algorithm that facilitates our method to find repeating subsequences of length 2 or more -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Output
1 |
|
Of course, the time and space complexity is O(n^2), but from the first method, it is more elegant and easy to produce O(1) hash.
Improved solution
In this approach we will try to make some observations based on the previous approach.
Observation 1 − If a character appears more than twice, the answer is always true. Let's see why?
Example - In the string str = "AVHJFBABVNHFA" we have "AAA" in positions 0, 6 and 12. so We can have "AA" at index 0 and 6 as a subsequence, and "AA" at index 6 and 12 as a subsequence as another.
Observation 2 - If a character is repeated only once, it cannot contribute to our results subsequence, since it will only be available in at most one subsequence. it won't work Because we need at least two subsequences. So we can remove or ignore all characters happened at the same time.
Observation 3 − If a string is a palindrome, and the first two observations apply, then the answer is Always false unless the string length is odd. Let's see why?
Example - String str = "PNDDNP"
Explanation - Now, the characters are not in order, so we can never find The subsequences have the same order, so this is not possible.
Example
Based on all three observations above, we conclude that if we remove all characters that appear once in the string and then check if a certain character appears more than twice or if the string is not a palindrome, then we The answer is correct. Let’s see the improved solution implemented in C -
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
Output
1 |
|
in conclusion
We concluded that using observations and hashes is the best way to solve this problem. The time complexity is O(n). Space complexity is also on the order of O(n), creating a new string and a constant 26 character hash.
The above is the detailed content of C++ program to find if a given string has a repeating subsequence of length 2 or more. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

What happens when you turn off Find My on iPhone? Find My iPhone helps you locate a lost or stolen device. When enabled, Find My iPhone lets you track your device's location on a map, plays sounds, and helps you find your device. Find My also includes an Activation Lock to prevent anyone from using your iPhone. When you turn off Find My iPhone, you lose all these features, which may make recovering a lost Apple device difficult. While Find My iPhone is very useful, you should disable it when you want to sell, donate, trade in your phone, or send it for battery replacement or any other service. Doing this will ensure that no one can access information about you

Use the Array.IndexOf function in C# to find the index of an element in an array. In a C# program, when we need to find the index of an element in an array, we can use the Array.IndexOf function. The Array.IndexOf function finds the specified element within the specified array range and returns the index of its first occurrence. If the element is not found, -1 is returned. The following is a sample code that demonstrates how to use the Array.IndexOf function to find an element in an array.

Hard drive serial numbers and MAC addresses are important identifiers in computer hardware and are very useful in managing and maintaining computer systems. This article will introduce how to find the hard disk serial number and MAC address. 1. Find the hard drive serial number. The hard drive serial number is a unique identifier used by the hard drive manufacturer to identify and track the hard drive. In different operating systems, the method of finding the hard drive serial number is slightly different. Windows: Open Command Prompt (search for "cmd" in the Start menu) and enter the following command and press Enter: wmicdisk

Apple's Find My app allows you to locate your iPhone or other device to prevent it from being lost or forgotten. While Find My is a useful tool for tracking devices, you may want to disable it if you're concerned about privacy issues, don't want to drain your battery, or for other reasons. Fortunately, there are several ways to turn off Find My on iPhone, all of which we will explain in this article. How to Turn off Find My on iPhone [4 Methods] You can turn off Find My on iPhone in four ways. If you used Method 1 to turn off Find, you can do this from the device you want to disable it on. To proceed with methods 2, 3, and 4, the iPhone that you want to turn off Find Finder should be powered off or

Hyperbolic functions are defined using hyperbolas instead of circles and are equivalent to ordinary trigonometric functions. It returns the ratio parameter in the hyperbolic sine function from the supplied angle in radians. But do the opposite, or in other words. If we want to calculate an angle from a hyperbolic sine, we need an inverse hyperbolic trigonometric operation like the hyperbolic inverse sine operation. This course will demonstrate how to use the hyperbolic inverse sine (asinh) function in C++ to calculate angles using the hyperbolic sine value in radians. The hyperbolic arcsine operation follows the following formula -$$\mathrm{sinh^{-1}x\:=\:In(x\:+\:\sqrt{x^2\:+\:1})}, Where\:In\:is\:natural logarithm\:(log_e\:k)

The rename function changes a file or directory from its old name to its new name. This operation is similar to the move operation. So we can also use this rename function to move files. This function exists in the stdio.h library header file. The syntax of the rename function is as follows: intrename(constchar*oldname,constchar*newname); The function of the rename() function accepts two parameters. One is oldname and the other is newname. Both parameters are pointers to constant characters that define the old and new names of the file. Returns zero if the file was renamed successfully; otherwise, returns a nonzero integer. During a rename operation

The glob() function in PHP is used to find files or directories and is a powerful file operation function. It can return the path of a file or directory based on a specified pattern match. The syntax of the glob() function is as follows: glob(pattern, flags) where pattern represents the pattern string to be matched, which can be a wildcard expression, such as *.txt (matching files ending with .txt), or a specific file path. flags is an optional parameter used to control the function

A map is a special type of container in C++ in which each element is a pair of two values, namely a key value and a map value. The key value is used to index each item, and the mapped value is the value associated with the key. Regardless of whether the mapped value is unique, the key is always unique. To print map elements in C++ we have to use iterator. An element in a set of items is indicated by an iterator object. Iterators are primarily used with arrays and other types of containers (such as vectors), and they have a specific set of operations that can be used to identify specific elements within a specific range. Iterators can be incremented or decremented to reference different elements present in a range or container. The iterator points to the memory location of a specific element in the range. Printing a map in C++ using iterators First, let's look at how to define
