Transform the given String into two same Strings by removing at most one character
Given a string S of length N where (N ? 1). The task is to check whether by removing at most one character from the given string it can be divided into two same sub-strings or not return YES or NO for possibilities.
Examples:
Input: N = 5, S = abzab
Output: YES
Explanation: Character ‘z’ at index 3 can be removed so that the remaining S will be “abab”. Which consists of two same sub-strings “ab” and “ab”. Therefore the output is YES.
Input: N = 4, S = abcd
Output: NO
Explanation: It can be verified that a given string can’t be divided into two sub-strings by removing at most one character from it.
Approach: Follow the below observations to solve the problem:
Concept of approach:
The problem is observation based and can be divided into followings parts:
- If S has length equal to 1:
- In this case it is impossible to make two equal strings.
- If S has even length:
- It should be noted that removing one character from even length will make the S string’s length equal to odd, Then we can’t divide odd length string S into two strings having the same length. Formally, only even length S can be divided into two strings. Therefore, We can’t remove any character from even length String.
- Just divide the string into two halves and compare them using in-built function String. equals(String a, String b) of String class.
- If S has odd length:
- It is possible to make two sub-strings of equal length from odd length. For example, S = “ababd”, Then it can be divided into two strings of odd and even lengths as {“ab”, “bd”}, {“aba”, “abd”} respectively by removing one or zero character.
- Then just check for both parity sub-strings separately, If they differs by at most one character or not.
Follow the below steps to solve the above approach:
- If the input String has a length equal to 1.
- Then it is not possible to make two equal strings.
- If the input string has an even length
- Then divide the input string into two halves and check if they both are the same or not using the in-built String.equals(a, b) function.
- If the input string has an odd length
- Then divide the input string into two odd and two even parts and compare if there is at most one different character, Then it is possible else not possible.
Below is the Implementation of the above approach:
C++
#include <iostream>
#include <string>
using namespace std;
bool check(string a, string b)
{
int l = 0;
int k = 0;
int countdiff = 0;
while (l < a.length()) {
if (k < b.length() && a[l] == b[k]) {
l++;
k++;
}
else {
l++;
countdiff++;
}
}
return countdiff <= 1;
}
int main()
{
int N = 5;
string s = "ababz" ;
if (s.length() == 1) {
cout << "NO" << endl;
}
else if (s.length() % 2 == 0) {
string fir = s.substr(0, (s.length() / 2));
string sec = s.substr((s.length() / 2));
if (fir == sec) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
else {
string fir = s.substr(0, s.length() / 2 + 1);
string sec = s.substr(s.length() / 2 + 1);
string third = s.substr(s.length() / 2);
string fourth = s.substr(0, s.length() / 2);
if (check(fir, sec) || check(third, fourth)) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
}
return 0;
}
|
Java
import java.io.*;
import java.math.*;
import java.util.*;
public class GFG {
public static boolean check(String a, String b)
{
int l = 0 ;
int k = 0 ;
int countdiff = 0 ;
while (l < a.length()) {
if (k < b.length()
&& a.charAt(l) == b.charAt(k)) {
l++;
k++;
}
else {
l++;
countdiff++;
}
}
return countdiff <= 1 ;
}
public static void main(String args[])
{
int N = 5 ;
String s = "ababz" ;
if (s.length() == 1 ) {
System.out.println( "NO" );
}
else if (s.length() % 2 == 0 ) {
String fir = s.substring( 0 , (s.length() / 2 ));
String sec = s.substring((s.length() / 2 ));
if (fir.equals(sec)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
else {
String fir = s.substring( 0 , s.length() / 2 + 1 );
String sec = s.substring(s.length() / 2 + 1 );
String third = s.substring(s.length() / 2 );
String fourth = s.substring( 0 , s.length() / 2 );
if (check(fir, sec) || check(third, fourth)) {
System.out.println( "YES" );
}
else {
System.out.println( "NO" );
}
}
}
}
|
Python3
def check(a: str , b: str ) - > bool :
l = 0
k = 0
countdiff = 0
while l < len (a):
if k < len (b) and a[l] = = b[k]:
l + = 1
k + = 1
else :
l + = 1
countdiff + = 1
return countdiff < = 1
def main():
N = 5
s = "ababz"
if len (s) = = 1 :
print ( "NO" )
elif len (s) % 2 = = 0 :
fir = s[ 0 : len (s) / / 2 ]
sec = s[ len (s) / / 2 :]
if fir = = sec:
print ( "YES" )
else :
print ( "NO" )
else :
fir = s[ 0 : len (s) / / 2 + 1 ]
sec = s[ len (s) / / 2 + 1 :]
third = s[ len (s) / / 2 :]
fourth = s[ 0 : len (s) / / 2 ]
if check(fir, sec) or check(third, fourth):
print ( "YES" )
else :
print ( "NO" )
if __name__ = = "__main__" :
main()
|
C#
using System;
using System.Linq;
public class GFG {
public static bool Check( string a, string b)
{
int l = 0;
int k = 0;
int countdiff = 0;
while (l < a.Length) {
if (k < b.Length && a[l] == b[k]) {
l++;
k++;
}
else {
l++;
countdiff++;
}
}
return countdiff <= 1;
}
static public void Main()
{
int N = 5;
string s = "ababz" ;
if (s.Length == 1) {
Console.WriteLine( "NO" );
}
else if (s.Length % 2 == 0) {
string fir = s.Substring(0, s.Length / 2);
string sec = s.Substring(s.Length / 2);
if (fir.Equals(sec)) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
else {
string fir = s.Substring(0, s.Length / 2 + 1);
string sec = s.Substring(s.Length / 2 + 1);
string third = s.Substring(s.Length / 2);
string fourth = s.Substring(0, s.Length / 2);
if (Check(fir, sec) || Check(third, fourth)) {
Console.WriteLine( "YES" );
}
else {
Console.WriteLine( "NO" );
}
}
}
}
|
Javascript
function Check(a, b)
{
let l = 0;
let k = 0;
let countdiff = 0;
while (l < a.length) {
if (k < b.length && a[l] == b[k]) {
l++;
k++;
}
else {
l++;
countdiff++;
}
}
return countdiff <= 1;
}
function Main()
{
let N = 5;
let s = "ababz" ;
if (s.length == 1) {
console.log( "NO" );
}
else if (s.length % 2 == 0) {
let fir = s.substring(0, s.Length / 2);
let sec = s.substring(s.Length / 2);
if (fir.localeCompare(sec)) {
console.log( "YES" );
}
else {
console.log( "NO" );
}
}
else {
let fir = s.substring(0, s.Length / 2 + 1);
let sec = s.substring(s.Length / 2 + 1);
let third = s.substring(s.Length / 2);
let fourth = s.substring(0, s.Length / 2);
if (Check(fir, sec) || Check(third, fourth)) {
console.log( "YES" );
}
else {
console.log( "NO" );
}
}
}
Main()
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Related Articles:
Last Updated :
22 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...