找到最大回文子串_使用O(1)空间复杂度找到最大的回文子串
找到最大回文子串
Problem statement:
問題陳述:
Given a string, you have to find the largest palindromic substring using O(1) space complexity.
給定一個字符串,您必須使用O(1)空間復雜度找到最大的回文子字符串。
Input:T Test caseT no of input string will be given to you.E.g.3abcsouuoshgcbaincludeaedulcinaaaaaConstrain 1≤ length (string) ≤100Output:Print the largest palindromic substring form the given string.Example
例
T=3Input:abcsouuoshgcbaOutput:souuosInput:includeaedulcinOutput:cludeaedulcInput:aaaaaOutput:aaaaaExplanation with example:
舉例說明:
Let there is a string str.
讓我們有一個字符串str 。
Now possible arrangements are:
現在可能的安排是:
Single middle characters like aba
像aba這樣的單個中間字符
Paired middle characters like bb
配對的中間字符,如bb
To find the largest palindromic substring we follow these steps,
要找到最大的回文子串,請按照以下步驟操作,
We start with the first index and go to the end of the string.
我們從第一個索引開始,然后到字符串的末尾。
Every time we take two variables
每次我們取兩個變量
For the first possible arrangement, we initialize the first variable with the previous index of the current index and initialize the second variable with the next index of the current index.
對于第一種可能的安排,我們使用當前索引的上一個索引初始化第一個變量,并使用當前索引的下一個索引初始化第二個變量。
For the second possible arrangement, we initialize the first variable with the current index and initialize the second variable with the next variable of the current index.
對于第二種可能的安排,我們用當前索引初始化第一個變量,并用當前索引的下一個變量初始化第二個變量。
If the character at the first variable place is equal with the character at the second variable place then every time we decrease the first index by one and increase the second index by one and continue the process until or unless the first variable value will be greater than or equals to zero and the second variable value will be less than the length of the string.
如果第一個變量位置的字符與第二個變量位置的字符相等,則每次我們將第一個索引減小一個,然后將第二個索引增大一個,然后繼續執行該過程,直到或除非第一個變量值大于或等于零,并且第二個變量值將小于字符串的長度。
Every time we will add up two with the length of the palindrome substring count.
每次我們將用回文子串計數的長度加起來兩個。
If the character at both the variable place is not the same then we compare with the palindrome substring length.
如果兩個變量位置的字符都不相同,則將其與回文子串的長度進行比較。
C++ Implementation:
C ++實現:
#include <bits/stdc++.h> using namespace std;string count_palindrom(string str) {int len = str.length();int max_length = 0;int start = 0;for (int i = 0; i < len; i++) {int j = i - 1;int k = i + 1;int count = 1;while (j >= 0 && k < len) {if (str[j] == str[k]) {count += 2;if (max_length < count) {max_length = count;start = j;}j--;k++;}else {break;}}j = i;k = i + 1;count = 0;while (j >= 0 && k < len) {if (str[j] != str[k])break;else {count += 2;if (max_length < count) {max_length = count;start = j;}j--;k++;}}}string s = "";if (start == 0 && max_length == 0) {return s + str[0];}for (int i = 0; i < max_length; i++) {s += str[start + i];}return s; }int main() {//codeint t;cout << "Test Case : ";cin >> t;while (t--) {string str;cout << "Enter the string : ";cin >> str;cout << "The palindromic substrings is : " << count_palindrom(str) << endl;}return 0; }Output
輸出量
Test Case : 3 Enter the string : abcsouuoshgcba The palindromic substrings is : souuos Enter the string : includeaedulcin The palindromic substrings is : cludeaedulc Enter the string : aaaaa The palindromic substrings is : aaaaa翻譯自: https://www.includehelp.com/icp/find-the-largest-palindromic-substring-using-o-1-space-complexity.aspx
找到最大回文子串
總結
以上是生活随笔為你收集整理的找到最大回文子串_使用O(1)空间复杂度找到最大的回文子串的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 原神风龙废墟怎么开启
- 下一篇: 杭州治子宫内膜异位症最好的医院推荐