# Minimum operation require to make first and last character same

Given a string **S**. You are allowed two types of operations:

- Remove a character from the front of the string.
- Remove a character from the end of the string.

The task is to find the minimum operations required to make the first and last character of the S same. In case, it is not possible, print “-1”.

**Examples:**

Input : S = "bacdefghipalop" Output : 4 Remove 'b' from the front and remove 'p', 'o', 'l' from the end of the string S. Input : S = "pqr" Output : -1

The idea is to find the first and last occurrences of each character in the string. The total amount of operations needed will be simply “number of operations needed to remove the first occurrence” plus “number of operations needed to remove the last occurrence”. So, do this for eah character in the string and the answer will be minimum of such operations performed on each character.

For example, S = “zabcdefghaabbbb”, calculate the operations required to have character ‘a’ at both the front and the end, meaning to say the string “a….a”. For the minimum number of operations, we will form the string “abcdefghaa” i.e we will remove one character ‘z’ from front and 4 characters ‘bbbb’ from back. Hence total 5 operations will be required.

So, apply the above algorithm for each character and hence we can then find the minimum of those operations.

Below is implementation of this approach:

## C++

`// C++ program to find minimum operation ` `// require to make first and last character same ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MAX 256 ` ` ` `// Return the minimum operation require ` `// to make string first and last character same. ` `int` `minimumOperation(string s) ` `{ ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Store indexes of first occurrences of characters. ` ` ` `vector<` `int` `> first_occ(MAX, -1); ` ` ` ` ` `// Initialize result ` ` ` `int` `res = INT_MAX; ` ` ` ` ` `// Traverse through all characters ` ` ` `for` `(` `int` `i=0; i<n; i++) ` ` ` `{ ` ` ` `// Find first occurrence ` ` ` `char` `x = s[i]; ` ` ` `if` `(first_occ[x] == -1) ` ` ` `first_occ[x] = i; ` ` ` ` ` `// Update result for subsequent occurrences ` ` ` `else` ` ` `{ ` ` ` `int` `last_occ = (n-i-1); ` ` ` `res = min(res, first_occ[x] + last_occ); ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driven Program ` `int` `main() ` `{ ` ` ` `string s = ` `"bacdefghipalop"` `; ` ` ` `cout << minimumOperation(s) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum ` `// operation require to make ` `// first and last character same ` ` ` `import` `java.util.*; ` `import` `java.lang.*; ` `import` `java.io.*; ` ` ` `class` `GFG{ ` ` ` `static` `final` `int` `MAX=` `256` `; ` ` ` `// Return the minimum operation requires to ` `// make string first and last character same. ` `static` `int` `minimumOperation(String s) ` `{ ` ` ` `int` `n = s.length(); ` ` ` ` ` `// Store indexes of first occurrences of characters. ` ` ` `Vector<Integer> first_occ=` `new` `Vector<Integer>(); ` ` ` ` ` `//Initialize all the elements to -1 ` ` ` `for` `(` `int` `i=` `0` `;i<MAX;i++) ` ` ` `first_occ.add(i,-` `1` `); ` ` ` ` ` `// Initialize result ` ` ` `int` `res = Integer.MAX_VALUE; ` ` ` ` ` `// Traverse through all characters ` ` ` `for` `(` `int` `i=` `0` `; i<n; i++) ` ` ` `{ ` ` ` `// Find first occurrence ` ` ` `int` `x = (` `int` `)s.charAt(i); ` ` ` `if` `(first_occ.elementAt(x) == -` `1` `) ` ` ` `first_occ.set(x,i); ` ` ` ` ` `// Update result for subsequent occurrences ` ` ` `else` ` ` `{ ` ` ` `int` `last_occ = (n-i-` `1` `); ` ` ` `res = Math.min(res, first_occ.get(x) + last_occ); ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driven Program ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `String s = ` `"bacdefghipalop"` `; ` ` ` `System.out.println(minimumOperation(s)); ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find minimum operation ` `# require to make first and last character same ` `MAX` `=` `256` ` ` `# Return the minimum operation require to ` `# make string first and last character same. ` `def` `minimumOperation(s): ` ` ` ` ` `n ` `=` `len` `(s) ` ` ` ` ` `# Store indexes of first ` ` ` `# occurrences of characters. ` ` ` `first_occ ` `=` `[` `-` `1` `] ` `*` `MAX` ` ` ` ` `# Initialize result ` ` ` `res ` `=` `float` `(` `'inf'` `) ` ` ` ` ` `# Traverse through all characters ` ` ` `for` `i ` `in` `range` `(` `0` `, n): ` ` ` ` ` `# Find first occurrence ` ` ` `x ` `=` `s[i] ` ` ` `if` `first_occ[` `ord` `(x)] ` `=` `=` `-` `1` `: ` ` ` `first_occ[` `ord` `(x)] ` `=` `i ` ` ` ` ` `# Update result for subsequent occurrences ` ` ` `else` `: ` ` ` `last_occ ` `=` `n ` `-` `i ` `-` `1` ` ` `res ` `=` `min` `(res, first_occ[` `ord` `(x)] ` `+` `last_occ) ` ` ` ` ` `return` `res ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `s ` `=` `"bacdefghipalop"` ` ` `print` `(minimumOperation(s)) ` ` ` `# This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum ` `// operation require to make ` `// first and last character same ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `MAX = 256; ` ` ` `// Return the minimum operation requires to ` `// make string first and last character same. ` `static` `int` `minimumOperation(String s) ` `{ ` ` ` `int` `n = s.Length; ` ` ` ` ` `// Store indexes of first occurrences of characters. ` ` ` `List<` `int` `> first_occ = ` `new` `List<` `int` `>(); ` ` ` ` ` `//Initialize all the elements to -1 ` ` ` `for` `(` `int` `i = 0; i < MAX; i++) ` ` ` `first_occ.Insert(i,-1); ` ` ` ` ` `// Initialize result ` ` ` `int` `res = ` `int` `.MaxValue; ` ` ` ` ` `// Traverse through all characters ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `// Find first occurrence ` ` ` `int` `x = (` `int` `)s[i]; ` ` ` `if` `(first_occ[x] == -1) ` ` ` `first_occ.Insert(x,i); ` ` ` ` ` `// Update result for subsequent occurrences ` ` ` `else` ` ` `{ ` ` ` `int` `last_occ = (n - i - 1); ` ` ` `res = Math.Min(res, first_occ[x] + last_occ); ` ` ` `} ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String []args) ` `{ ` ` ` `String s = ` `"bacdefghipalop"` `; ` ` ` `Console.WriteLine(minimumOperation(s)); ` `} ` `} ` ` ` `// This code contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

4

Time Complexity : O(n)

## Recommended Posts:

- Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character
- Using Counter() in Python to find minimum character removal to make two strings anagram
- Remove a character from a string to make it a palindrome
- Transform One String to Another using Minimum Number of Given Operation
- Number of character corrections in the given strings to make them equal
- Print the final string when minimum value strings get concatenated in every operation
- Minimum cost to make two strings same
- Minimum K such that every substring of length atleast K contains a character c
- Minimum changes to a string to make all substrings distinct
- Minimum removal to make palindrome permutation
- Minimum Cost To Make Two Strings Identical
- Minimum number of deletions to make a string palindrome | Set 2
- Minimum number of pairs required to make two strings same
- Minimum number of deletions to make a string palindrome
- Minimum number of Parentheses to be added to make it valid

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.