


Checks whether a string can be split into three substrings, where one substring is a substring of the other two substrings
In this problem, we need to split the given string so that the third substring can be a substring of the first two substrings.
Let's think of a solution. The third string can be a substring of the first two strings only if the first two strings contain all the characters of the third string. So, we need to find at least one character with frequency greater than 3 in the given string, and we can take the third substring of that single character.
Problem Statement - We are given a string str containing N lowercase alphabetic characters. We need to check if we can split the string into three substrings a, b and c such that substring c is a substring of a and b. Print "yes" or "no" depending on whether 3 substrings can be found.
Example
1 |
|
1 |
|
illustrate
Here, we can split the string into "tu", "torialsPoin" and "t". Therefore, the third string is a substring of the first two strings.
1 |
|
1 |
|
illustrate
We cannot split the string into three substrings based on the given condition because the frequency of any character is not greater than 3.
1 |
|
1 |
|
illustrate
According to the given conditions, the three substrings can be 'h', 'h' and 'hhhhhh'.
method 1
In this method, we will use an array to store the frequency of each character. After that, we will check for characters with frequency greater than or equal to 3.
algorithm
Step 1 - Define the "freq" array with length equal to 26.
Step 2 - Loop through the string to count the frequency of characters. In the for loop, increment the value of freq[str[i] – ‘a’]. Here, str[i] – ‘a’ gives an index between 0 and 26.
Step 3 - Now, loop through the "freq" array and return true if the value at any array index is greater than "3".
Step 4 - Return true when the loop terminates.
Step 5 - Print "Yes" or "No" based on the return value of the isSUbStringPossible() function.
Example
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 |
|
Output
1 |
|
Time complexity - O(N), when we iterate over the string.
Space complexity - O(1) since we use constant length arrays.
Method 2
In this method, we first convert the string into a character array. After that, we use the count() method to count the frequency of specific characters in the array.
algorithm
Step 1 - Create an array of size "len 1", where "len" is the string length.
Step 2 - Use the strcpy() method to copy the string into a char array.
Step 3 - Use a for loop for a total of 26 iterations.
Step 4 - In a for loop, use the count() method to count the number of occurrences of a specific character.
The Step 5 - Returns true if the count() method returns greater than or equal to 3.
Step 6 - Return false when the loop terminates.
count() method takes a reference to the starting position as the first argument, a reference to the ending position as the second argument, and a character as the third argument.
Here, we need to pass the ASCII value of the character as a parameter and we use I ‘a’ to get the value.
Example
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 |
|
Output
1 |
|
Time complexity - O(N) because count() method iterates over char array to count the number of characters. Also, the strcpy() method takes O(N) time.
Space complexity - O(N) since we store the string into a character array.
in conclusion
We learned two ways to split a string into three substrings, so that one substring can become a substring of two other substrings. The code of the second method is more readable and friendly to beginners, but it is more expensive in time and space.
The above is the detailed content of Checks whether a string can be split into three substrings, where one substring is a substring of the other two substrings. 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


![Spellcheck not working in Teams [Fixed]](https://img.php.cn/upload/article/000/887/227/170968741326618.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
We've started noticing that sometimes spellcheck stops working for Teams. Spell check is an essential tool for effective communication, and any attack on it can cause considerable disruption to workflow. In this article, we'll explore common reasons why spell check might not be working as expected, and how to restore it to its previous state. So, if spell check is not working in Teams, follow the solutions mentioned in this article. Why doesn't Microsoft spell check work? There may be several reasons why Microsoft spell check is not working properly. These reasons include incompatible language settings, disabled spell check function, damaged MSTeam or MSOffice installation, etc. Also, outdated MSTeams and MSOf

The program being executed is called a process. A process can be an application running on the current operating system or an application related to the operating system. If an application is tied to the operating system, it first creates a process to execute itself. Other applications rely on operating system services for execution. Most applications are operating system services and background applications that maintain the operating system, software, and hardware. In python we have different methods to check if application is open or not. Let’s learn about them in detail one by one. Using the psutil.process_iter() function psutil is a module in Python that provides users with an interface to retrieve information about running processes and system utilization.

An iterable object is an object whose all elements can be iterated over using a loop or iterable function. Lists, strings, dictionaries, tuples, etc. are all called iterable objects. In Python language, there are various ways to check whether an object is iterable. Let’s take a look one by one. Using Loops In Python, we have two looping techniques, one is using "for" loop and the other is using "while" loop. Using either of these two loops, we can check if a given object is iterable. Example In this example, we will try to iterate an object using "for" loop and check if it is iterated or not. Below is the code. l=["apple",22,"orang

Given two strings str_1 and str_2. The goal is to count the number of occurrences of substring str2 in string str1 using a recursive procedure. A recursive function is a function that calls itself within its definition. If str1 is "Iknowthatyouknowthatiknow" and str2 is "know" the number of occurrences is -3. Let us understand through examples. For example, input str1="TPisTPareTPamTP", str2="TP"; output Countofoccurrencesofasubstringrecursi

How to use the LOCATE function in MySQL to find the position of a substring in a string. In MySQL, there are many functions that can be used to process strings. Among them, the LOCATE function is a very useful function that can be used to find the position of a substring in a string. The syntax of the LOCATE function is as follows: LOCATE(substring,string,[position]) where substring is the substring to be found and string is the substring to be found.

How to check SSD health status in Windows 11? For their fast read, write, and access speeds, SSDs are quickly replacing HDDs, but even though they are more reliable, you still need to check the health of your SSDs in Windows 11. How to operate it? In this tutorial, the editor will share with you the method. Method 1: Use WMIC1, use the key combination Win+R, type wmic, and then press or click OK. Enter2. Now, type or paste the following command to check the SSD health status: diskdrivegetstatus If you receive the "Status: OK" message, your SSD drive is operating normally.

This function is similar to the strtok() function. The only key difference is _r, which is called a reentrant function. Reentrant functions are functions that can be interrupted during execution. This type of function can be used to resume execution. Therefore, reentrant functions are thread-safe, which means they can be safely interrupted by threads without causing any damage. The strtok_r() function has an extra parameter called context. This way the function can be restored in the correct location. The syntax of the strtok_r() function is as follows: #include<string.h>char*strtok_r(char*string,constchar*limiter,char**

You can use the contains() method of the List interface to check whether an object exists in the list. contains() method booleancontains(Objecto) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null?e==null:o.equals(e)). Parameter c - the element whose presence in this list is to be tested. Return Value Returns true if this list contains the specified element. Throws ClassCastException - if the specified element's type is incompatible with this list (optional). NullP
