Open In App

Find uncommon characters of the two strings

Last Updated : 16 Sep, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Basic
38.13%
91.5K

Find and print the uncommon characters of the two given strings in sorted order. Here uncommon character means that either the character is present in one string or it is present in another string but not in both. The strings contain only lowercase characters and can contain duplicates. 

Examples: 

Input: str1 = “characters”, str2 = “alphabets” 
Output: b c l p r

Input: str1 = “geeksforgeeks”, str2 = “geeksquiz” 
Output: f i o q r u z


[Naive Approach] O(n^2) Time and O(1) Space:

Using two loops, for each character of 1st string check whether it is present in the 2nd string or not. Likewise, for each character of 2nd string check whether it is present in the 1st string or not.

Note: In the practice area of gfg the string has to be sorted in order to match with the output. 

Below is the implementation of the above approach:

C++
// C++ implementation to find the uncommon
// characters of the two strings
#include <bits/stdc++.h>
using namespace std;

// function to find the uncommon characters
// of the two strings
string UncommonChars(string str1, string str2) {
  
    // to store the answer
    string ans = "";
    
      // to handle the case of duplicates
    vector<int> used(26, false);

    // check first for str1
    for (int i = 0; i < str1.size(); i++) {
      
        // keeping a flag variable
        bool found = false;

        for (int j = 0; j < str2.size(); j++) {
          
            // if found change the flag
            // and break from loop
            if (str1[i] == str2[j]) {
                found = true;
                break;
            }
        }

        // if duplicate character not found
        // then add it to ans
        if (!found and !used[str1[i] - 'a']) {
            used[str1[i] - 'a'] = true;
            ans += str1[i];
        }
    }

    // now check for str2
    for (int i = 0; i < str2.size(); i++) {
      
        // keeping a flag variable
        bool found = false;

        for (int j = 0; j < str1.size(); j++) {
          
            // if found change the flag
            // and break from loop
            if (str2[i] == str1[j]) {
                found = true;
                break;
            }
        }

        // if duplicate character not found
        // then add it to ans
        if (!found and !used[str2[i] - 'a']) {
            used[str2[i] - 'a'] = true;
            ans += str2[i];
        }
    }

    // to match with output
    sort(ans.begin(), ans.end());

      // if not found any character
    if (ans.size() == 0)
        return "-1";
    
      // else print the answer
      else
        return ans;
}

int main() {
    string str1 = "characters";
    string str2 = "alphabets";
    cout<<UncommonChars(str1, str2);
    return 0;
}
Java
import java.util.Arrays;

public class UncommonCharacters {

    // Function to find the uncommon characters of the two strings
    public static String uncommonChars(String str1, String str2) {
        StringBuilder ans = new StringBuilder();

        // to handle the case of duplicates
        boolean[] used = new boolean[26];

        // check first for str1
        for (int i = 0; i < str1.length(); i++) {
            boolean found = false;

            for (int j = 0; j < str2.length(); j++) {
                if (str1.charAt(i) == str2.charAt(j)) {
                    found = true;
                    break;
                }
            }

            if (!found && !used[str1.charAt(i) - 'a']) {
                used[str1.charAt(i) - 'a'] = true;
                ans.append(str1.charAt(i));
            }
        }

        // now check for str2
        for (int i = 0; i < str2.length(); i++) {
            boolean found = false;

            for (int j = 0; j < str1.length(); j++) {
                if (str2.charAt(i) == str1.charAt(j)) {
                    found = true;
                    break;
                }
            }

            if (!found && !used[str2.charAt(i) - 'a']) {
                used[str2.charAt(i) - 'a'] = true;
                ans.append(str2.charAt(i));
            }
        }

        // sort the answer
        char[] ansArray = ans.toString().toCharArray();
        Arrays.sort(ansArray);

        // if not found any character
        if (ansArray.length == 0)
            return "-1";

        return new String(ansArray);
    }

    public static void main(String[] args) {
        String str1 = "characters";
        String str2 = "alphabets";
        System.out.println(uncommonChars(str1, str2));
    }
}
Python
def uncommon_chars(str1, str2):
    ans = ""

    # to handle the case of duplicates
    used = [False] * 26

    # check first for str1
    for i in range(len(str1)):
        found = False

        for j in range(len(str2)):
            if str1[i] == str2[j]:
                found = True
                break

        if not found and not used[ord(str1[i]) - ord('a')]:
            used[ord(str1[i]) - ord('a')] = True
            ans += str1[i]

    # now check for str2
    for i in range(len(str2)):
        found = False

        for j in range(len(str1)):
            if str2[i] == str1[j]:
                found = True
                break

        if not found and not used[ord(str2[i]) - ord('a')]:
            used[ord(str2[i]) - ord('a')] = True
            ans += str2[i]

    ans = ''.join(sorted(ans))

    if len(ans) == 0:
        return "-1"

    return ans

if __name__ == "__main__":
    str1 = "characters"
    str2 = "alphabets"
    print(uncommon_chars(str1, str2))
C#
using System;
using System.Collections.Generic;
using System.Linq;

public class Program {
    public static string UncommonChars(string str1, string str2) {
        string ans = "";

        // to handle the case of duplicates
        bool[] used = new bool[26];

        // check first for str1
        for (int i = 0; i < str1.Length; i++) {
            bool found = false;

            for (int j = 0; j < str2.Length; j++) {
                if (str1[i] == str2[j]) {
                    found = true;
                    break;
                }
            }

            if (!found && !used[str1[i] - 'a']) {
                used[str1[i] - 'a'] = true;
                ans += str1[i];
            }
        }

        // now check for str2
        for (int i = 0; i < str2.Length; i++) {
            bool found = false;

            for (int j = 0; j < str1.Length; j++) {
                if (str2[i] == str1[j])
                {
                    found = true;
                    break;
                }
            }

            if (!found && !used[str2[i] - 'a']) {
                used[str2[i] - 'a'] = true;
                ans += str2[i];
            }
        }

        // sort the answer
        char[] ansArray = ans.ToCharArray();
        Array.Sort(ansArray);

        if (ansArray.Length == 0)
            return "-1";

        return new string(ansArray);
    }

    public static void Main(string[] args)
    {
        string str1 = "characters";
        string str2 = "alphabets";
        Console.WriteLine(UncommonChars(str1, str2));
    }
}
JavaScript
function uncommonChars(str1, str2) {
    let ans = "";

    // to handle the case of duplicates
    let used = Array(26).fill(false);

    // check first for str1
    for (let i = 0; i < str1.length; i++) {
        let found = false;

        for (let j = 0; j < str2.length; j++) {
            if (str1[i] == str2[j]) {
                found = true;
                break;
            }
        }

        if (!found && !used[str1.charCodeAt(i) - 'a'.charCodeAt(0)]) {
            used[str1.charCodeAt(i) - 'a'.charCodeAt(0)] = true;
            ans += str1[i];
        }
    }

    // now check for str2
    for (let i = 0; i < str2.length; i++) {
        let found = false;

        for (let j = 0; j < str1.length; j++) {
            if (str2[i] == str1[j]) {
                found = true;
                break;
            }
        }

        if (!found && !used[str2.charCodeAt(i) - 'a'.charCodeAt(0)]) {
            used[str2.charCodeAt(i) - 'a'.charCodeAt(0)] = true;
            ans += str2[i];
        }
    }

    // sort the answer
    ans = ans.split('').sort().join('');

    if (ans.length === 0)
        return "-1";

    return ans;
}

let str1 = "characters";
let str2 = "alphabets";
console.log(uncommonChars(str1, str2));

Output
bclpr

Time Complexity: O(n1*n2)
Auxiliary Space:  O(1), as a constant-size array is used to handle duplicates.

[Optimized Approach] Using Hashing O(n) Time and O(1) Space:

The approach involves using a hash table to keep track of character presence from both strings. First, iterate through the first string and mark each character’s presence in the hash table. Then, iterate through the second string, updating the hash table to mark characters already seen in the first string as common and new characters as unique to the second string. Finally, collect and sort all characters marked as unique to either string, and return them; if no such characters are found, return “-1”.

The below image is a dry run of the above approach:


Below is the implementation of the above approach:

C++
// C++ implementation to find the uncommon
// characters of the two strings
#include <bits/stdc++.h>
using namespace std;

// size of the hash table
const int MAX_CHAR = 26;

// function to find the uncommon characters
// of the two strings
 string UncommonChars(string str1, string str2)
{
    // mark presence of each character as 0
    // in the hash table 'present[]'
      string ans="";
    int present[MAX_CHAR];
    for (int i=0; i<MAX_CHAR; i++)
        present[i] = 0;

    int l1 = str1.size();
    int l2 = str2.size();

    // for each character of str1, mark its
    // presence as 1 in 'present[]'
    for (int i=0; i<l1; i++)
        present[str1[i] - 'a'] = 1;

    // for each character of str2
    for (int i=0; i<l2; i++)
    {
        // if a character of str2 is also present
        // in str1, then mark its presence as -1
        if (present[str2[i] - 'a'] == 1
            || present[str2[i] - 'a'] == -1)
            present[str2[i] - 'a'] = -1;

        // else mark its presence as 2
        else
            present[str2[i] - 'a'] = 2;
    }

    // print all the uncommon characters
    for (int i=0; i<MAX_CHAR; i++)
        if (present[i] == 1 || present[i] == 2 )
            ans.push_back(char(i+'a'));
    if(ans.size()==0)
    {
        return "-1";
    }
  return ans;
}
// Driver program to test above
int main()
{
    string str1 = "characters";
    string str2 = "alphabets";
    cout<<UncommonChars(str1, str2);
    return 0;
}
Java
public class GfG {
    static final int MAX_CHAR = 26;

    public static String uncommonChars(String str1, String str2) {
        StringBuilder ans = new StringBuilder();
        int[] present = new int[MAX_CHAR];
        for (int i = 0; i < MAX_CHAR; i++) {
            present[i] = 0;
        }

        int l1 = str1.length();
        int l2 = str2.length();

        for (int i = 0; i < l1; i++) {
            present[str1.charAt(i) - 'a'] = 1;
        }

        for (int i = 0; i < l2; i++) {
            if (present[str2.charAt(i) - 'a'] == 1 || present[str2.charAt(i) - 'a'] == -1) {
                present[str2.charAt(i) - 'a'] = -1;
            } else {
                present[str2.charAt(i) - 'a'] = 2;
            }
        }

        for (int i = 0; i < MAX_CHAR; i++) {
            if (present[i] == 1 || present[i] == 2) {
                ans.append((char)(i + 'a'));
            }
        }

        if (ans.length() == 0) {
            return "-1";
        }
        return ans.toString();
    }

    public static void main(String[] args) {
        String str1 = "characters";
        String str2 = "alphabets";
        System.out.println(uncommonChars(str1, str2));
    }
}
Python
def uncommon_chars(str1, str2):
    MAX_CHAR = 26
    ans = ""
    present = [0] * MAX_CHAR

    for char in str1:
        present[ord(char) - ord('a')] = 1

    for char in str2:
        if present[ord(char) - ord('a')] == 1 or present[ord(char) - ord('a')] == -1:
            present[ord(char) - ord('a')] = -1
        else:
            present[ord(char) - ord('a')] = 2

    for i in range(MAX_CHAR):
        if present[i] == 1 or present[i] == 2:
            ans += chr(i + ord('a'))

    if len(ans) == 0:
        return "-1"
    return ans

if __name__ == "__main__":
    str1 = "characters"
    str2 = "alphabets"
    print(uncommon_chars(str1, str2))
C#
using System;

public class UncommonCharacters {
    static int MAX_CHAR = 26;

    static string UncommonChars(string str1, string str2) {
        string ans = "";
        int[] present = new int[MAX_CHAR];
        for (int i = 0; i < MAX_CHAR; i++) {
            present[i] = 0;
        }

        int l1 = str1.Length;
        int l2 = str2.Length;

        for (int i = 0; i < l1; i++)
        {
            present[str1[i] - 'a'] = 1;
        }

        for (int i = 0; i < l2; i++) {
            if (present[str2[i] - 'a'] == 1 || present[str2[i] - 'a'] == -1) {
                present[str2[i] - 'a'] = -1;
            }
            else {
                present[str2[i] - 'a'] = 2;
            }
        }

        for (int i = 0; i < MAX_CHAR; i++) {
            if (present[i] == 1 || present[i] == 2)
            {
                ans += (char)(i + 'a');
            }
        }

        if (ans.Length == 0)
        {
            return "-1";
        }
        return ans;
    }

    static void Main(string[] args) {
        string str1 = "characters";
        string str2 = "alphabets";
        Console.WriteLine(UncommonChars(str1, str2));
    }
}
JavaScript
function uncommonChars(str1, str2) {
    const MAX_CHAR = 26;
    let ans = "";
    let present = new Array(MAX_CHAR).fill(0);

    for (let i = 0; i < str1.length; i++) {
        present[str1.charCodeAt(i) - 'a'.charCodeAt(0)] = 1;
    }

    for (let i = 0; i < str2.length; i++) {
        if (present[str2.charCodeAt(i) - 'a'.charCodeAt(0)] == 1 || present[str2.charCodeAt(i) - 'a'.charCodeAt(0)] == -1) {
            present[str2.charCodeAt(i) - 'a'.charCodeAt(0)] = -1;
        } else {
            present[str2.charCodeAt(i) - 'a'.charCodeAt(0)] = 2;
        }
    }

    for (let i = 0; i < MAX_CHAR; i++) {
        if (present[i] == 1 || present[i] == 2) {
            ans += String.fromCharCode(i + 'a'.charCodeAt(0));
        }
    }

    if (ans.length === 0) {
        return "-1";
    }
    return ans;
}

let str1 = "characters";
let str2 = "alphabets";
console.log(uncommonChars(str1, str2));

Output
bclpr

Time Complexity: O(m + n), where m and n are the sizes of the two strings respectively.
Auxiliary Space: O(1), no any other extra space is required, so it is a constant.



Similar Reads

Find resultant string after concatenating uncommon characters of given strings
Given two strings S1 and S2. The task is to concatenate uncommon characters of the S2 to S1 and return the resultant string S1 . Examples: Input: S1 = "aacdb", S2 = "gafd"Output: "cbgf" Input: S1 = "abcs", S2 = "cxzca";Output: "bsxz" Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution. Method 1: Using Hashmap Approac
13 min read
Concatenated string with uncommon characters in Python
Two strings are given and you have to modify the 1st string such that all the common characters of the 2nd string have to be removed and the uncommon characters of the 2nd string have to be concatenated with the uncommon characters of the 1st string. Examples: Input : S1 = "aacdb", S2 = "gafd"Output : "cbgf"Input : S1 = "abcs";, S2 = "cxzca";Output
4 min read
Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50} Output : 10 25 40 50 We do not print 20 and 30 as these elements are present in both arrays. Input : arr1[] = {10, 20
6 min read
Javascript Program to Print uncommon elements from two sorted arrays
Given two sorted arrays of distinct elements, we need to print those elements from both arrays that are not common. The output should be printed in sorted order. Examples : Input : arr1[] = {10, 20, 30} arr2[] = {20, 25, 30, 40, 50}Output : 10 25 40 50We do not print 20 and 30 as theseelements are present in both arrays.Input : arr1[] = {10, 20, 30
2 min read
Check if given strings can be made same by swapping two characters of same or different strings
Given an array of equal-length strings, arr[] of size N, the task is to check if all the strings can be made equal by repeatedly swapping any pair of characters of same or different strings from the given array. If found to be true, then print "YES". Otherwise, print "NO". Examples: Input: arr[] = { "acbdd", "abcee" } Output: YES Explanation: Swapp
11 min read
Longest Uncommon Subsequence
Given two strings, find the length of longest uncommon subsequence of the two strings. The longest uncommon subsequence is defined as the longest subsequence of one of these strings which is not a subsequence of other strings. Examples: Input : "abcd", "abc"Output : 4The longest subsequence is 4 because "abcd"is a subsequence of first string, but n
12 min read
Shortest Uncommon Subsequence
Given two strings S and T, find the length of the shortest subsequence in S which is not a subsequence in T. If no such subsequence is possible, return -1. A subsequence is a sequence that appears in the same relative order, but is not necessarily contiguous. A string of length n has 2^n different possible subsequences. String S of length m (1 <
14 min read
Count of 3 length strings using given characters containing at least 2 different characters
Given three integers a, b and c which denotes the frequencies of three different characters 'A', 'B', and 'C' respectively, and can be used to form strings of length 3. The task is to count the total number of possible combinations of A, B and C such that it forms a string having at least 2 different characters. Example: Input: a = 2, b = 3, c = 3O
6 min read
Minimize swaps of same-indexed characters to make sum of ASCII value of characters of both the strings odd
Given two N-length strings S and T consisting of lowercase alphabets, the task is to minimize the number of swaps of the same indexed elements required to make the sum of the ASCII value of characters of both the strings odd. If it is not possible to make the sum of ASCII values odd, then print "-1". Examples: Input:S = ”acd”, T = ”dbf”Output: 1Exp
9 min read
Find two unique Palindrome Strings using given String characters
You are given a string S of two distinct characters as input, the task is to find two strings using characters of the given string, such that both strings are different from S and both should be a palindrome. Examples: Input: S = "xyyxxxx"Output: yxxxxxy, xxyxyxxExplanation: It can be verified that at least one of the output strings is different fr
15+ min read
Count new pairs of strings that can be obtained by swapping first characters of pairs of strings from given array
Given an array arr[] consisting of N strings, the task is to find the pair of strings that is not present in the array formed by any pairs (arr[i], arr[j]) by swapping the first characters of the strings arr[i] and arr[j]. Examples: Input: arr[] = {"good", "bad", "food"}Output: 2Explanation:The possible pairs that can be formed by swapping the firs
13 min read
Convert given Strings into T by replacing characters in between strings any number of times
Given an array, arr[] of N strings and a string T of size M, the task is to check if it is possible to make all the strings in the array arr[] same as the string T by removing any character from one string, say arr[i] and inserting it at any position to another string arr[j] any number of times. Examples: Input: arr[] = {"abc", "abb", "acc"}, T = "
9 min read
Make all Strings palindrome by swapping characters from adjacent Strings
Given an array of strings S[] of size X where each string has length N. In one move, you can swap the ith character of two adjacent strings, the task is to find the minimum number of moves to make every string a palindrome. If this is not possible, print -1. Examples: Input: S[] = {"13", "21", "32"}Output: 2?Explanation: We make all the strings pal
10 min read
Count of strings in Array A that can be made equal to strings in Array B by appending characters
Given two arrays of strings SearchWord[] and FindWord[]. The task is to check how many strings in FindWord[] can be formed after the following operations on as many strings as possible: Pick a string from SearchWord[].Add one English alphabet (that is not present in that string).Rearrange the string as you like.Note: Every alphabet in the SearchWor
13 min read
Test Case Generation | Set 2 ( Random Characters, Strings and Arrays of Random Strings)
Set 1 (Random Numbers, Arrays and Matrices) Generating Random Characters C/C++ Code // A C++ Program to generate test cases for // random characters #include<bits/stdc++.h> using namespace std; // Define the number of runs for the test data // generated #define RUN 5 // Define the range of the test data generated // Here it is 'a' to 'z' #def
11 min read
Rearrange the characters of the string such that no two adjacent characters are consecutive English alphabets
Given string str of size N consists of lower-case English alphabets. The task is to find the arrangement of the characters of the string such that no two adjacent characters are neighbors in English alphabets. In case of multiple answers print any of them. If no such arrangement is possible then print -1.Examples: Input: str = "aabcd" Output: bdaac
6 min read
Minimize swaps of pairs of characters required such that no two adjacent characters in the string are same
Given a string S consisting of N characters, the task is to find the minimum number of pairs of characters that are required to be swapped such that no two adjacent characters are the same. If it is not possible to do so, then print "-1". Examples: Input: S = "ABAACD"Output: 1Explanation: Swapping S[3] and S[4] modifies the given string S to "ABACA
9 min read
Rearrange characters in a String such that no two adjacent characters are same
Given a string with lowercase repeated characters, the task is to rearrange characters in a string so that no two adjacent characters are the same. If it is not possible to do so, then print "Not possible". Examples: Input: aaabc Output: abaca Input: aaabbOutput: ababa Input: aa Output: Not Possible Input: aaaabc Output: Not Possible Asked In: Amaz
15+ min read
Print common characters of two Strings in alphabetical order
Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), k(1 time) and s(1 time). Hence the lexicographical out
6 min read
Python code to print common characters of two Strings in alphabetical order
Given two strings, print all the common characters in lexicographical order. If there are no common letters, print -1. All letters are lower case. Examples: Input : string1 : geeks string2 : forgeeks Output : eegks Explanation: The letters that are common between the two strings are e(2 times), g(1 time), k(1 time) and s(1 time). Hence the lexicogr
2 min read
Count common characters in two strings
Given two strings s1 and s2 consisting of lowercase English alphabets, the task is to count all the pairs of indices (i, j) from the given strings such that s1[i] = s2[j] and all the indices are distinct i.e. if s1[i] pairs with some s2[j] then these two characters will not be paired with any other character.Example Input: s1 = "abcd", s2 = "aad" O
5 min read
Compare two strings considering only alphanumeric characters
Given two strings that can contain lower and uppercase alphabets, numbers and special characters like dots, blank spaces, commas, etc. Compare both strings considering only alphanumeric characters([a-b], [A-B] and [0-9]) if they are equal or not. For example, strings "Ram, Shyam" and "Ram-Shyam" both are the same and also "/.';[]" and "@# >" are
10 min read
Check whether two strings contain same characters in same order
Given two strings s1 and s2, the task is to find whether the two strings contain the same characters that occur in the same order. For example string "Geeks" and string "Geks" contain the same characters in same order. Examples: Input: s1 = "Geeks", s2 = "Geks" Output: Yes Input: s1 = "Arnab", s2 = "Andrew" Output: No Approach: We have two strings
9 min read
Check whether two strings can be made equal by copying their characters with the adjacent ones
Given two strings str1 and str2, the task is to check whether both of the string can be made equal by copying any character of the string with its adjacent character. Note that this operation can be performed any number of times.Examples: Input: str1 = "abc", str2 = "def" Output: No As all the characters in both the string are different. So, there
5 min read
Minimum characters to be deleted from the beginning of two strings to make them equal
Given two strings S and T, the task is to find the minimum number of characters to be deleted from the beginning of these strings to make the two strings identical. Two empty strings will always be an identical strings. Examples: Input: S = "geeksforgeeks" T = "peeks" Output: 10 Explanation: Substring "geeksforg" from S and "p" from T are deleted t
4 min read
Minimum characters to be deleted from the end to make given two strings equal
Given two strings S1 and S2, the task is to find the minimum number of characters to be deleted from the end of the two strings to make them equal. Two empty strings will be considered identical. Examples: Input: S1 = "abcd", S2 = "absefr" Output: 6 Explanation: Characters {'d', 'c'} are deleted from the end of S1 and characters {'r', 'f', 'e', 's'
4 min read
Check if a string can be split into two strings with same number of K-frequent characters
Given a string S and an integer K, the task is to check if it is possible to distribute these characters into two strings such that the count of characters having a frequency K in both strings is equal. If it is possible, then print a sequence consisting of 1 and 2, which denotes which character should be placed in which string. Otherwise, print NO
11 min read
Count ways to place all the characters of two given strings alternately
Given two strings, str1 of length N and str2 of length M of distinct characters, the task is to count the number of ways to place all the characters of str1 and str2 alternatively. Note: |N - M| ? 1 Examples: Input: str1 =“ae”, str2 = “bd”Output: 8Explanations:Possible strings after rearrangements are : {“abed”, “ebad”, “adeb”, “edab”, “bade”, “bed
5 min read
Minimize replacements or swapping of same indexed characters required to make two given strings palindromic
Given two strings, str1 and str2 consisting of N lowercase alphabets, the task is to find the minimum count of operations of the following two types to make both the strings palindromic string. Replace any character of the strings to any other character([a - z]).Swap any two characters present at the same index in both the strings. Examples: Input:
8 min read
Check if two binary strings can be made equal by swapping pairs of unequal characters
Given two binary strings S1 and S2 of length N (1 ? N ? 105), the task is to check if it is possible to convert the string S1 to S2 by performing the following operations any number of times: Select any two indices i and j (1 ? i < j ? N) such that S1[i] is ‘0’ and S1[j] is ‘1’.Swap S1[i] with S1[j].Examples: Input: S1 ="100111", S2 = "111010" O
12 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg