Write an algorithm to help Arnold find the character that was missing in C

1). A company wishes to encode its data. The data is in the form of a string and is case sensitive. Theywish to encode the data with respect to a specific character. They wish to count the number of times thecharacter reoccurs in the given data so that they can encode the data accordingly.Write an algorithm to find the count of the specter in the given data.

Today we are going to discuss some Wipro Elite Coding Questions that has been asked in previous years in Wipro for Project Engineer role for Freshers.

So, If you are appearing in Wipro Elite, then there are fair chances that you can get same difficulty level of questions in your actual exam.

Write an algorithm to help Arnold find the character that was missing in C
Wipro elite coding problem 5

Hi all, Welcome to Jobs Adda. Here you would get all fresher jobs updates and placement material for engineering graduates related to IT/Core sector.

To get all job updates on time, Don’t forget to join our whatsapp group. Click below to join.

Write an algorithm to help Arnold find the character that was missing in C
Write an algorithm to help Arnold find the character that was missing in C

A company provides network encryption for secure data transfer. The data string is encrypted prior to transmission and gets decrypted at the receiving end. But due to some technical error, the encrypted data is lost and the received string is different from the original string by 1 character. Arnold, a network administrator, is tasked with finding the character that got lost in the network so that the bug does not harm other data that is being transferred through the network. Write an algorithm to help Arnold find the character that was missing at the receiving end but present at the sending end.

Input
The input consists of two space-separated strings – stringSentand stringRec, representing the string that was sent through the network, and the string that was received at the receiving end of the network, respectively.

Output
Print a character representing the character that was lost in the network during transmission and if there is no data loss during transmission then print “NA”.

Constraints
NA

Example

Input:
abcdfjgerj abcdfijger

Output:
j

Explanation:
The character ‘j’ at the end of the sent string was lost in the network during transmission.

Solution: In C

#include <stdio.h> int main() { char s[1000],r[1000]; scanf("%s %s", s,r); int i; for(i=0;s[i]!='\0';i++) { if(s[i]!=r[i]) { printf("%c",s[i]); break; } } if(s[i]=='\0') printf("NA"); }

You're Reading a Free Preview
Pages 6 to 12 are not shown in this preview.

If String a = "abbc" and String b="abc", we have to print that character 'b' is missing in the second string.

I want to do it by using Java. I am able to do it when String 2 has a character not present in String 1 when s1=abc and s2=abk but not when characters are same in both strings like the one I have mentioned in the question.

public class Program { public static void main(String[] args) { String str1 = "abbc"; String str2 = "abc"; char first[] = str1.toCharArray(); char second[] = str2.toCharArray(); HashMap <Character, Integer> map1 = new HashMap<Character,Integer>(); for(char a: first){ if(!map1.containsKey(a)){ map1.put(a,1); }else{ map1.put(a,map1.get(a)+1); } } System.out.println(map1); HashMap <Character, Integer> map2 = new HashMap<Character,Integer>(); for(char b: second){ if(!map2.containsKey(b)){ map2.put(b,1); }else{ map2.put(b,map2.get(b)+1); } } System.out.println(map2); } }

I have two hashmaps here one for the longer string and one for the shorter string, map1 {a=1,b=2,c=1} and map2 {a=1,b=1,c=1}. What should I do after this?

Given two strings which are of lengths n and n+1. The second string contains all the character of the first string, but there is one extra character. Your task to find the extra character in the second string.
Examples : 
 

Input : string strA = "abcd"; string strB = "cbdae"; Output : e string B contain all the element there is a one extra character which is e Input : string strA = "kxml"; string strB = "klxml"; Output : l string B contain all the element there is a one extra character which is l

Method 1(Brute Force):- Check with two for loop. Time Complexity:- O(n^2) Space Complexity:- O(1). 

Method 2(Hash Map):- 

Create an empty hash table and insert all character of second string. Now remove all characters of first string. Remaining character is the extra character.Time Complexity:- O(n) Auxiliary Space:- O(n). 

char findExtraCharcter(string strA, string strB)

    unordered_map<char, int> m1;

    for (int i = 0; i < strB.length(); i++)

    for (int i = 0; i < strA.length(); i++)

    for (auto h1 = m1.begin(); h1 != m1.end(); h1++) {

    cout << findExtraCharcter(strA, strB);

static char findExtraCharcter(char []strA, char[] strB)

    for (int i = 0; i < strB.length; i++)

    for (int i = 0; i < strA.length; i++)

    for (int i=0;i<m1.length;i++)

    return Character.MIN_VALUE;

public static void main(String[] args)

    System.out.println(findExtraCharcter(strA.toCharArray(), strB.toCharArray()));

def findExtraCharacter(strA, strB):

if __name__ == "__main__":

    print(findExtraCharacter(strA, strB))

static char findExtraCharcter(char []strA, char[] strB)

    for (int i = 0; i < strB.Length; i++)

    for (int i = 0; i < strA.Length; i++)

    for (int i = 0; i < m1.Length; i++)

public static void Main(String[] args)

    Console.WriteLine(findExtraCharcter(strA.ToCharArray(),

function findExtraCharcter(strA,strB)

     for(let i = 0; i < 256; i++)

    for (let i = 0; i < strB.length; i++)

        m1[strB[i].charCodeAt(0)]++;

    for (let i = 0; i < strA.length; i++)

        m1[strA[i].charCodeAt(0)]--;

    for (let i = 0; i < m1.length; i++)

            return String.fromCharCode(i);

document.write(findExtraCharcter(strA.split(""), strB.split("")));

Method 3(Bits):- traverse first and second string from starting with xor operation at the end you get the character which is extra. Time Complexity:- O(n+n+1) Space Complexity:- O(1).



char findExtraCharcter(string strA, string strB)

    for (i = 0; i < strA.length(); i++) {

    for (i = 0; i < strB.length(); i++) {

    cout << findExtraCharcter(strA, strB);

    static char findExtraCharcter(String strA,

        for (i = 0; i < strA.length(); i++)

        for (i = 0; i < strB.length(); i++)

    public static void main(String args[])

        System.out.println(findExtraCharcter(strA, strB));

def findExtraCharcter(strA, strB) :

    for i in range(0,len(strA)) :

        res =res ^ (ord)(strA[i])

    for i in range(0,len(strB)) :

        res = res ^ (ord)(strB[i])

print(findExtraCharcter(strA, strB))

    static char findExtraCharcter(string strA,

        for (i = 0; i < strA.Length; i++) {

        for (i = 0; i < strB.Length; i++) {

    public static void Main()

            findExtraCharcter(strA, strB));

function findExtraCharcter($strA, $strB)

    for ($i = 0; $i < strlen($strA); $i++)

    for ($i = 0; $i < strlen($strB); $i++)

echo chr(findExtraCharcter($strA, $strB));

function findExtraCharcter(strA, strB)

    for (let i = 0; i < strA.length; i++)

        res ^= strA.charCodeAt(i);

    for (let i = 0; i < strB.length; i++)

        res ^= strB.charCodeAt(i);

document.write(String.fromCharCode(findExtraCharcter(strA, strB)));

Method 4(Character Code):- Add the character codes of both the strings. Minus character codes of smaller string from larger string and convert the result integer into character.

Time Complexity:- O(n) 


Auxiliary Space:- O(1). 
 

char findExtraCharacter(string s1, string s2)

    if(s1.size() > s2.size())

    int smallStrCodeTotal = 0;

    int largeStrCodeTotal = 0;

    for(; i < smallStr.size(); i++)

        smallStrCodeTotal += smallStr[i];

        largeStrCodeTotal += largeStr[i];

    largeStrCodeTotal += largeStr[i];

    int intChar = largeStrCodeTotal - smallStrCodeTotal;    

    char extraChar = findExtraCharacter(s1, s2);

    cout<<"Extra character: " <<(extraChar)<<endl;

    private static char findExtraCharacter(String s1, String s2) {       

        if(s1.length() > s2.length()) {

        int smallStrCodeTotal = 0;

        int largeStrCodeTotal = 0;

        for(; i < smallStr.length(); i++) {

            smallStrCodeTotal += smallStr.charAt(i);

            largeStrCodeTotal += largeStr.charAt(i);

        largeStrCodeTotal += largeStr.charAt(i);

        int intChar = largeStrCodeTotal - smallStrCodeTotal;       

    public static void main(String[] args) {

        char extraChar = findExtraCharacter(s1, s2);

        System.out.println("Extra character: " + extraChar);

def findExtraCharacter(s1,s2):

    while(i < len(smallStr)):

        smallStrCodeTotal += ord(smallStr[i])

        largeStrCodeTotal += ord(largeStr[i])

    largeStrCodeTotal += ord(largeStr[i])

    intChar = largeStrCodeTotal - smallStrCodeTotal

extraChar = findExtraCharacter(s1, s2)

print("Extra Character:", extraChar)

    private static char findExtraCharacter(String s1,

        if(s1.Length > s2.Length)

        int smallStrCodeTotal = 0;

        int largeStrCodeTotal = 0;

        for(; i < smallStr.Length; i++)

            smallStrCodeTotal += smallStr[i];

            largeStrCodeTotal += largeStr[i];

        largeStrCodeTotal += largeStr[i];

        int intChar = largeStrCodeTotal -

    public static void Main(String[] args)

        char extraChar = findExtraCharacter(s1, s2);

        Console.WriteLine("Extra character: " +

function findExtraCharacter(s1, s2)

        if(s1.length > s2.length)

        let smallStrCodeTotal = 0;

        let largeStrCodeTotal = 0;

        for(; i < smallStr.length; i++)

            smallStrCodeTotal += smallStr[i].charCodeAt(0);

            largeStrCodeTotal += largeStr[i].charCodeAt(0);

        largeStrCodeTotal += largeStr[i].charCodeAt(0);

        let intChar = largeStrCodeTotal - smallStrCodeTotal;  

        return String.fromCharCode(intChar);

let extraChar = findExtraCharacter(s1, s2);

document.write("Extra character: " + extraChar);

Output:  Extra character: e


Article Tags :

Practice Tags :