给定长度为N的字符串S仅包含字符‘a’ , ‘b’和‘c’ ,任务是通过仅执行一次以下操作来最小化给定字符串的长度:
- 将字符串分为两个非空子字符串,然后将左子字符串附加到右子字符串的末尾。
- 追加时,如果右子字符串的后缀和左子字符串的前缀包含相同的字符,则分别从右子字符串和左子字符串的后缀和前缀中删除这些字符。重复此步骤,直到没有不可删除的子字符串。
例子:
Input: S = “aabcccabba”
Output: 4
Explanation:
Divide the given string S in two parts as “aabcc” and “cabba”. Below are the operations performed on the above two substrings:
- Remove the prefixes and suffixes of the same characters, i.e. ‘a’. The string becomes “bcc” and “cabb”.
- Remove the suffixes and prefixes of the same characters, i.e. ‘b’. The string becomes “cc” and “ca”.
Now, after concatenating the right and left substrings, the string obtained is “cacc”, which is of the shortest length, i.e. 4.
Input: S = “aacbcca”
Output: 1
方法:给定的问题可以通过使用两个指针通过寻找类似的字符出现在字符串的后缀和字符串的前缀来解决。
请按照以下步骤解决问题:
- 初始化两个变量,比如说i为0和j为(N – 1) 。
- 重复循环直到i
并且字符S [i]和S [j]相同,然后执行以下步骤: - 初始化一个变量,例如用S [i]表示d ,然后在i最多为j且d = S [i]时将指针i向右移动。
- 向左移动指针j ,直到i最多为j且d = S [j] 。
- 完成上述步骤后, (j – i + 1)的值是字符串的最小可能长度。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the minimum length
// of the string after removing the same
// characters from the end and front of the
// two strings after dividing into 2 substrings
int minLength(string s)
{
// Initialize two pointers
int i = 0, j = s.length() - 1;
// Traverse the string S
for(; i < j && s[i] == s[j];)
{
// Current char on left pointer
char d = s[i];
// Shift i towards right
while (i <= j && s[i] == d)
i++;
// Shift j towards left
while (i <= j && s[j] == d)
j--;
}
// Return the minimum possible
// length of string
return j - i + 1;
}
// Driver Code
int main()
{
string S = "aacbcca";
cout << minLength(S);
}
// This code is contributed by bgangwar59
Java
// Java program for the above approach
import java.lang.*;
import java.util.*;
class GFG {
// Function to find the minimum length
// of the string after removing the same
// characters from the end and front of the
// two strings after dividing into 2 substrings
static int minLength(String s)
{
// Initialize two pointers
int i = 0, j = s.length() - 1;
// Traverse the string S
for (; i < j
&& s.charAt(i) == s.charAt(j);) {
// Current char on left pointer
char d = s.charAt(i);
// Shift i towards right
while (i <= j
&& s.charAt(i) == d)
i++;
// Shift j towards left
while (i <= j
&& s.charAt(j) == d)
j--;
}
// Return the minimum possible
// length of string
return j - i + 1;
}
// Driver Code
public static void main(String[] args)
{
String S = "aacbcca";
System.out.println(minLength(S));
}
}
Python3
# Python3 program for the above approach
# Function to find the minimum length
# of the string after removing the same
# characters from the end and front of the
# two strings after dividing into 2 substrings
def minLength(s):
# Initialize two pointers
i = 0; j = len(s) - 1
# Traverse the string S
while (i < j and s[i] == s[j]):
# Current char on left pointer
d = s[i]
# Shift i towards right
while (i <= j and s[i] == d):
i += 1
# Shift j towards left
while (i <= j and s[j] == d):
j -= 1
# Return the minimum possible
# length of string
return j - i + 1
# Driver Code
if __name__ == "__main__" :
S = "aacbcca"
print(minLength(S))
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
class GFG{
// Function to find the minimum length
// of the string after removing the same
// characters from the end and front of the
// two strings after dividing into 2 substrings
static int minLength(string s)
{
// Initialize two pointers
int i = 0, j = s.Length - 1;
// Traverse the string S
for(; i < j && s[i] == s[j];)
{
// Current char on left pointer
char d = s[i];
// Shift i towards right
while (i <= j && s[i] == d)
i++;
// Shift j towards left
while (i <= j && s[j] == d)
j--;
}
// Return the minimum possible
// length of string
return j - i + 1;
}
// Driver Code
public static void Main(string[] args)
{
string S = "aacbcca";
Console.WriteLine(minLength(S));
}
}
// This code is contributed by AnkThon
输出:
1
时间复杂度: O(N)
辅助空间: O(N)