Minimum deletion such that Xor of adjacent digits is atmost 1
Given a string S consisting of N digits, the task is to find the minimum number of deletions such that the bitwise Xor of any two adjacent digits of the remaining string is at most 1.
Examples:
Input: S = “24244”
Output: 2

Explanation: We can delete both the 2’s to make the string “444”, which satisfies the condition.Input: S = “9999”
Output: 0
Approach: The problem can be solved based on the following observations:
It is known that bitwise XOR of same number is 0 and bitwise XOR of consecutive even and odd (odd greater than even) is always one i.e. 4 ⊕ 5 = 1. So we have remove all the elements which separate the elements that can form such a pair.
Follow the steps mentioned below to implement the idea.
- Iterate a loop over the string to count the frequency of characters and check character at ith position is odd or even:
- If the character is even then increment the frequency count.
- Otherwise, perform bitwise XOR with 1 and count the frequency of the result.
- Set max = 0 and check whether the frequency count of the character is greater than max, if it is true then set max = freqCount.
- After that find (n – max) which is the minimum number of deletions performed on the string.
Below is the implementation of the above approach:
C++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find the minimum number // of deletions performed on string int minDeletion( int arr[], int n) { unordered_map< int , int > mp; for ( int i = 0; i < n; i++) { mp[arr[i]]++; } int ans = 0; for ( auto i : mp) { if (i.first % 2 == 0) { if (mp.find(i.first + 1) != mp.end()) { ans = max(ans, (i.second + mp[i.first + 1])); } } ans = max(ans, i.second); } return n - ans; } // Driver Code int main() { string S = "24244" ; int A[S.length()]; for ( int i = 0; i < S.length(); i++) { A[i] = S.at(i); } int N = sizeof (A) / sizeof (A[0]); // Function Call cout << minDeletion(A, N); return 0; } |
Java
// Java code to implement the approach import java.io.*; import java.util.*; public class GFG { // Function to find the minimum number // of deletions performed on string public static int minDeletion( int arr[], int n) { HashMap<Integer, Integer> map = new HashMap<>(); for ( int i = 0 ; i < n; i++) { if (arr[i] % 2 == 0 ) { map.put(arr[i], map.getOrDefault(arr[i], 0 ) + 1 ); } else { map.put(arr[i] ^ 1 , map.getOrDefault(arr[i] ^ 1 , 0 ) + 1 ); } } int max = 0 ; for ( int freqCount : map.values()) { max = ( int )Math.max(freqCount, max); } int res = n - max; return res; } // Driver Code public static void main(String[] args) { String S = "24244" ; int [] A = new int [S.length()]; for ( int i = 0 ; i < S.length(); i++) { A[i] = S.charAt(i); } int N = A.length; // Function Call System.out.println(minDeletion(A, N)); } } |
Python3
# Python code to implement the approach # Function to find the minimum number # of deletions performed on string def minDeletion(arr, n): mp = {} for i in range (n): if (arr[i] in mp): mp[arr[i]] = mp[arr[i]] + 1 else : mp[arr[i]] = 1 ans = 0 for i,value in mp.items(): if (i % 2 = = 0 ): if ((i + 1 ) in mp): ans = max (ans, value + mp[i + 1 ]) ans = max (ans, value) return n - ans # Driver Code S = "24244" A = [ 0 ] * len (S) for i in range ( len (S)): A[i] = ord (S[i]) N = len (A) # Function Call print (minDeletion(A,N)) # This code is contributed by Pushpesh Raj |
C#
// C# code to implement the approach using System; using System.Collections.Generic; public class GFG { // Function to find the minimum number // of deletions performed on string public static int minDeletion( int [] arr, int n) { var mp = new Dictionary< int , int >(); for ( int i = 0; i < n; i++) { if (!mp.ContainsKey(arr[i])) { mp.Add(arr[i], 0); } mp[arr[i]]++; } int ans = 0; foreach ( var item in mp) { if (item.Key % 2 == 0) { if (mp.ContainsKey(item.Key + 1)) { ans = Math.Max( ans, (item.Value + mp[item.Key + 1])); } } ans = Math.Max(ans, item.Value); } return n - ans; } // Driver Code static public void Main() { string S = "24244" ; int [] A = new int [S.Length]; for ( int i = 0; i < S.Length; i++) { A[i] = ( int )S[i]; } int N = A.Length; // Function Call Console.WriteLine(minDeletion(A, N)); } } // This code is contributed by ksam24000 |
Javascript
function minDeletion(arr, n) { let mp = new Map(); for (let i=0;i<n;i++){ if (mp.has(arr[i])){ mp.set(arr[i],mp.get(arr[i])+1) } else { mp.set(arr[i],1) } } let ans = 0; for (let [key,val] of mp) { if (key % 2 == 0) { if (mp.has(key+ 1) ) { ans = max(ans, (val+ mp.get(key+ 1))); } } ans = Math.max(ans, val); } return n - ans; } // Driver Code let S = "24244" ; let A=[]; for (let i = 0; i < S.length; i++) { A.push(parseInt(S[i])); } let N = A.length; // Function Call console.log(minDeletion(A, N)); // This code is contributed by poojaagarwal2. |
2
Time Complexity: O(N)
Auxiliary Space: O(N)
Related Articles:
Please Login to comment...