给定一个长度为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 < j且字符S[i]和S[j]相同,执行以下步骤:
- 用S[i]初始化一个变量,比如d ,然后将指针i向右移动,而i至多是j并且d = S[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
Javascript
输出:
1
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live