通过重复删除具有不同开头和结尾的子字符串,最小移动以清空字符串
给定一个字符串str ,任务是通过删除任何开始和结束字符的子字符串来找到使str为空所需的最小移动次数 是不同的。如果无法清空字符串,则返回“ -1 ”。
例子:
Input: str = “abba”
Output: 2
Explanation: Follow the following steps to get the empty string: “abba” -> (delete ab) -> “ba” -> (delete ba) -> “”
Input: aba
Output: -1
Explanation: It is not possible to empty the given string.
Input: abc
Output: 1
方法:可以在观察的基础上解决任务。
- 如果第一个和最后一个字符不相等,则只需要一个操作
- 否则,检查是否存在与第一个和最后一个字符不匹配的任何2 个索引。如果它们存在,则所需的最小操作数为2 ,否则不可能将字符串缩减为空字符串。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the number of moves
// required to make the string empty.
void numberOfMoves(string& str, int n)
{
// If the first and the last
// character are different
if (str[0] != str[n - 1]) {
cout << "1";
return;
}
// Check if there is a index i such that
// s[i] != s[0] and s[i+1] != s[n-1]
for (int i = 0; i < n - 1; i++) {
if (str[i] != str[0]
&& str[i + 1] != str[n - 1]) {
cout << "2";
return;
}
}
// If no such index exists then
// the answer is -1
cout << -1;
return;
}
// Driver Code
int main()
{
string str = "abba";
int n = 4;
numberOfMoves(str, n);
}
Java
// Java code for the above approach
import java.io.*;
class GFG
{
// Function to find the number of moves
// required to make the string empty.
static void numberOfMoves(String str, int n)
{
// If the first and the last
// character are different
if (str.charAt(0) != str.charAt(n-1)) {
System.out.println("1");
return;
}
// Check if there is a index i such that
// s[i] != s[0] and s[i+1] != s[n-1]
for (int i = 0; i < n - 1; i++) {
if (str.charAt(i) != str.charAt(0)
&& str.charAt(i+1) != str.charAt(n-1)) {
System.out.println("2");
return;
}
}
// If no such index exists then
// the answer is -1
System.out.println(-1);
return;
}
// Driver Code
public static void main (String[] args) {
String str = "abba";
int n = 4;
numberOfMoves(str, n);
}
}
// This code is contributed by Potta Lokesh
Python3
# Pyhton program for the above approach
# Function to find the number of moves
# required to make the string empty.
def numberOfMoves(str, n):
# If the first and the last
# character are different
if (str[0] != str[n - 1]):
print("1")
return
# Check if there is a index i such that
# s[i] != s[0] and s[i+1] != s[n-1]
for i in range(0, len(str)):
if (str[i] != str[0] and
str[i + 1] != str[n - 1]):
print("2")
return
# If no such index exists then
# the answer is -1
print(-1)
return
# Driver Code
if __name__ == '__main__':
str = "abba"
n = 4;
numberOfMoves(str, n);
# This code is contributed by Samim Hossain Mondal.
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the number of moves
// required to make the string empty.
static void numberOfMoves(string str, int n)
{
// If the first and the last
// character are different
if (str[0] != str[n - 1]) {
Console.WriteLine("1");
return;
}
// Check if there is a index i such that
// s[i] != s[0] and s[i+1] != s[n-1]
for (int i = 0; i < n - 1; i++) {
if (str[i] != str[0]
&& str[i + 1] != str[n - 1]) {
Console.WriteLine("2");
return;
}
}
// If no such index exists then
// the answer is -1
Console.WriteLine(-1);
return;
}
// Driver Code
public static void Main(String []args) {
string str = "abba";
int n = 4;
numberOfMoves(str, n);
}
}
// This code is contributed by target_2.
Javascript
输出
2
时间复杂度: O(N)
辅助空间: O(1)。