Skip to main content

Find uncommon words in two strings

Here given code implementation process.

import java.util.HashMap;
/*
    Java program for
    Find uncommon words in two strings
*/
public class Dissimilar
{
    public void uncommonWords(String str1, String str2)
    {
        HashMap < String, Integer > record = 
          new HashMap < String, Integer > ();
        // Assume that string contains valid words
        String[] str1Words = str1.split(" ");
        String[] str2Words = str2.split(" ");
        boolean result = false;

        for (int i = 0; i < str1Words.length; ++i)
        {
            if (!record.containsKey(str1Words[i]))
            {
                // 1 indicates the word exist in first string
                record.put(str1Words[i], 1);
            }
        }
        for (int i = 0; i < str2Words.length; ++i)
        {
            if (record.containsKey(str2Words[i]) && 
                record.get(str2Words[i]) == 1)
            {
                // 3 indicates the word exist in both string
                record.put(str2Words[i], 3);
            }
            else
            {
                // 2 indicates the word exist in second string
                record.put(str2Words[i], 2);
            }
        }
        System.out.print("\n\n Given str1 : " + str1);
        System.out.print("\n Given str2 : " + str2);
        System.out.print("\n Result : ");
        // Display result
        for (String info: record.keySet())
        {
            if (record.get(info) != 3)
            {
                result = true;
                System.out.print("  " + info);
            }
        }
        if (result == false)
        {
            System.out.print("\n None ");
        }
    }
    public static void main(String[] args)
    {
        Dissimilar task = new Dissimilar();
        // Test A
        String str1 = "Test Mango orange pineapple and lemons";
        String str2 = "Mango is sweet but lemons are sour Test";
        task.uncommonWords(str1, str2);
        // Test B
        str1 = "Code One";
        str2 = "One Language";
        task.uncommonWords(str1, str2);
    }
}

Output

 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   orange  but  pineapple  are  and  is  sweet  sour

 Given str1 : Code One
 Given str2 : One Language
 Result :   Language  Code
// Include header file
#include <iostream>
#include <string.h>
#include <unordered_map>

using namespace std;
/*
    C++ program for
    Find uncommon words in two strings
*/
class Dissimilar
{
    public: void uncommonWords(char str1[], char str2[])
    {
        unordered_map < string, int > record;
        // Assume that string contains valid words
        char *str1Words = strtok(str1, " ");
        bool result = false;

        while (str1Words != NULL)
        {
            if (record.find(str1Words) == record.end())
            {
                // 1 indicates the word exist in first string
                record[str1Words] = 1;
            }
            str1Words = strtok(NULL, " ");
        }

        char *str2Words = strtok(str2, " ");
        while (str2Words!=NULL)
        {
            if (record.find(str2Words) != record.end() && record[str2Words] == 1)
            {
                // 3 indicates the word exist in both string
                record[str2Words] = 3;
            }
            else
            {
                // 2 indicates the word exist in second string
                record[str2Words] = 2;
            }
            str2Words = strtok(NULL, " ");
        }
    

        cout << "\n Result : ";
        // Display result
        for (auto &info: record)
        {
            if (info.second != 3)
            {
                result = true;
                cout << "  " << info.first;
            }
        }
        if (result == false)
        {
            cout << "\n None ";
        }
    }
};
int main()
{
    Dissimilar *task = new Dissimilar();
    // Test A
    char str1[] = "Test Mango orange pineapple and lemons";
    char str2[] = "Mango is sweet but lemons are sour Test";
    task->uncommonWords(str1, str2);
    // Test B
    char str3[] = "Code One";
    char str4[] = "One Language";
    task->uncommonWords(str3, str4);
    return 0;
}

Output

 Result :   pineapple  orange  and  is  are  sour  sweet  but
 Result :   Language  Code
// Include namespace system
using System;
using System.Collections.Generic;
/*
    Csharp program for
    Find uncommon words in two strings
*/
public class Dissimilar
{
	public void uncommonWords(String str1, String str2)
	{
		Dictionary < string, int > record = 
          new Dictionary < string, int > ();
		// Assume that string contains valid words
		String[] str1Words = str1.Split(" ");
		String[] str2Words = str2.Split(" ");
		Boolean result = false;
		for (int i = 0; i < str1Words.Length; ++i)
		{
			if (!record.ContainsKey(str1Words[i]))
			{
				// 1 indicates the word exist in first string
				record.Add(str1Words[i], 1);
			}
		}
		for (int i = 0; i < str2Words.Length; ++i)
		{
			if (record.ContainsKey(str2Words[i]) && record[str2Words[i]] == 1)
			{
				// 3 indicates the word exist in both string
				record[str2Words[i]] = 3;
			}
			else if(!record.ContainsKey(str2Words[i]))
			{
				// 2 indicates the word exist in second string
				record.Add(str2Words[i], 2);
			}
		}
		Console.Write("\n\n Given str1 : " + str1);
		Console.Write("\n Given str2 : " + str2);
		Console.Write("\n Result : ");
		// Display result
		foreach(KeyValuePair < String, int > info in record)
		{
			if (info.Value != 3)
			{
				result = true;
				Console.Write("  " + info.Key);
			}
		}
		if (result == false)
		{
			Console.Write("\n None ");
		}
	}
	public static void Main(String[] args)
	{
		Dissimilar task = new Dissimilar();
		// Test A
		String str1 = "Test Mango orange pineapple and lemons";
		String str2 = "Mango is sweet but lemons are sour Test";
		task.uncommonWords(str1, str2);
		// Test B
		str1 = "Code One";
		str2 = "One Language";
		task.uncommonWords(str1, str2);
	}
}

Output

 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   orange  pineapple  and  is  sweet  but  are  sour

 Given str1 : Code One
 Given str2 : One Language
 Result :   Code  Language
package main
import "strings"
import "fmt"
/*
    Go program for
    Find uncommon words in two strings
*/

func uncommonWords(str1, str2 string) {
	var record = make(map[string] int)
	// Assume that string contains valid words
	var str1Words = strings.Split(str1, " ")
	var str2Words = strings.Split(str2, " ")
	var result bool = false
	for i := 0 ; i < len(str1Words) ; i++ {
		if _, found := record[str1Words[i]] ; !found {
			// 1 indicates the word exist in first string
			record[str1Words[i]] = 1
		}
	}
	for i := 0 ; i < len(str2Words) ; i++ {
		if _, found := record[str2Words[i]] ; found && record[str2Words[i]] == 1 {
			// 3 indicates the word exist in both string
			record[str2Words[i]] = 3
		} else {
			// 2 indicates the word exist in second string
			record[str2Words[i]] = 2
		}
	}
	fmt.Print("\n\n Given str1 : ", str1)
	fmt.Print("\n Given str2 : ", str2)
	fmt.Print("\n Result : ")
	// Display result
	for k, v := range record {
		if v != 3 {
			result = true
			fmt.Print("  ", k,)
		}
	}
	if result == false {
		fmt.Print("\n None ")
	}
}
func main() {

	// Test A
	var str1 string = "Test Mango orange pineapple and lemons"
	var str2 string = "Mango is sweet but lemons are sour Test"
	uncommonWords(str1, str2)
	// Test B
	str1 = "Code One"
	str2 = "One Language"
	uncommonWords(str1, str2)
}

Output

 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   orange  but  pineapple  are  and  is  sweet  sour

 Given str1 : Code One
 Given str2 : One Language
 Result :   Language  Code
<?php
/*
    Php program for
    Find uncommon words in two strings
*/
class Dissimilar
{
	public	function uncommonWords($str1, $str2)
	{
		$record = array();
		// Assume that string contains valid words
		$str1Words = explode(" ", $str1);
		$str2Words = explode(" ", $str2);
		$result = false;
		for ($i = 0; $i < count($str1Words); ++$i)
		{
			if (!array_key_exists($str1Words[$i], $record))
			{
				// 1 indicates the word exist in first string
				$record[$str1Words[$i]] = 1;
			}
		}
		for ($i = 0; $i < count($str2Words); ++$i)
		{
			if (array_key_exists($str2Words[$i], $record) &&
                $record[$str2Words[$i]] == 1)
			{
				// 3 indicates the word exist in both string
				$record[$str2Words[$i]] = 3;
			}
			else
			{
				// 2 indicates the word exist in second string
				$record[$str2Words[$i]] = 2;
			}
		}
		echo("\n\n Given str1 : ".$str1);
		echo("\n Given str2 : ".$str2);
		echo("\n Result : ");
		// Display result
		foreach($record as $key => $value)
		{
			if ($value != 3)
			{
				$result = true;
				echo("  ".$key);
			}
		}
		if ($result == false)
		{
			echo("\n None ");
		}
	}
}

function main()
{
	$task = new Dissimilar();
	// Test A
	$str1 = "Test Mango orange pineapple and lemons";
	$str2 = "Mango is sweet but lemons are sour Test";
	$task->uncommonWords($str1, $str2);
	// Test B
	$str1 = "Code One";
	$str2 = "One Language";
	$task->uncommonWords($str1, $str2);
}
main();

Output

 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   orange  pineapple  and  is  sweet  but  are  sour

 Given str1 : Code One
 Given str2 : One Language
 Result :   Code  Language
/*
    Node JS program for
    Find uncommon words in two strings
*/
class Dissimilar
{
	uncommonWords(str1, str2)
	{
		var record = new Map();
		// Assume that string contains valid words
		var str1Words = str1.split(" ");
		var str2Words = str2.split(" ");
		var result = false;
		for (var i = 0; i < str1Words.length; ++i)
		{
			if (!record.has(str1Words[i]))
			{
				// 1 indicates the word exist in first string
				record.set(str1Words[i], 1);
			}
		}
		for (var i = 0; i < str2Words.length; ++i)
		{
			if (record.has(str2Words[i]) && record.get(str2Words[i]) == 1)
			{
				// 3 indicates the word exist in both string
				record.set(str2Words[i], 3);
			}
			else
			{
				// 2 indicates the word exist in second string
				record.set(str2Words[i], 2);
			}
		}
		process.stdout.write("\n\n Given str1 : " + str1);
		process.stdout.write("\n Given str2 : " + str2);
		process.stdout.write("\n Result : ");
		// Display result
		for (let [key, value] of record)
		{
			if (value != 3)
			{
				result = true;
				process.stdout.write("  " + key);
			}
		}
		if (result == false)
		{
			process.stdout.write("\n None ");
		}
	}
}

function main()
{
	var task = new Dissimilar();
	// Test A
	var str1 = "Test Mango orange pineapple and lemons";
	var str2 = "Mango is sweet but lemons are sour Test";
	task.uncommonWords(str1, str2);
	// Test B
	str1 = "Code One";
	str2 = "One Language";
	task.uncommonWords(str1, str2);
}
main();

Output

 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   orange  pineapple  and  is  sweet  but  are  sour

 Given str1 : Code One
 Given str2 : One Language
 Result :   Code  Language
#    Python 3 program for
#    Find uncommon words in two strings
class Dissimilar :
	def uncommonWords(self, str1, str2) :
		record = dict()
		#  Assume that string contains valid words
		str1Words = str1.split(" ")
		str2Words = str2.split(" ")
		result = False
		i = 0
		while (i < len(str1Words)) :
			if (not(str1Words[i] in record.keys())) :
				#  1 indicates the word exist in first string
				record[str1Words[i]] = 1
			
			i += 1
		
		i = 0
		while (i < len(str2Words)) :
			if ((str2Words[i] in record.keys()) and 
                record.get(str2Words[i]) == 1) :
				#  3 indicates the word exist in both string
				record[str2Words[i]] = 3
			else :
				#  2 indicates the word exist in second string
				record[str2Words[i]] = 2
			
			i += 1
		
		print("\n\n Given str1 : ", str1, end = "")
		print("\n Given str2 : ", str2, end = "")
		print("\n Result : ", end = "")
		for key, value in record.items() :
			if (value != 3) :
				result = True
				print(" ", key, end = "")
			
		
		if (result == False) :
			print("\n None ", end = "")
		
	

def main() :
	task = Dissimilar()
	#  Test A
	str1 = "Test Mango orange pineapple and lemons"
	str2 = "Mango is sweet but lemons are sour Test"
	task.uncommonWords(str1, str2)
	#  Test B
	str1 = "Code One"
	str2 = "One Language"
	task.uncommonWords(str1, str2)

if __name__ == "__main__": main()

Output

 Given str1 :  Test Mango orange pineapple and lemons
 Given str2 :  Mango is sweet but lemons are sour Test
 Result :   are  sweet  pineapple  but  orange  sour  is  and

 Given str1 :  Code One
 Given str2 :  One Language
 Result :   Code  Language
#    Ruby program for
#    Find uncommon words in two strings
class Dissimilar 
	def uncommonWords(str1, str2) 
		record = Hash.new()
		#  Assume that string contains valid words
		str1Words = str1.split(" ")
		str2Words = str2.split(" ")
		result = false
		i = 0
		while (i < str1Words.length) 
			if (!record.key?(str1Words[i])) 
				#  1 indicates the word exist in first string
				record[str1Words[i]] = 1
			end

			i += 1
		end

		i = 0
		while (i < str2Words.length) 
			if (record.key?(str2Words[i]) && record[str2Words[i]] == 1) 
				#  3 indicates the word exist in both string
				record[str2Words[i]] = 3
			else
 
				#  2 indicates the word exist in second string
				record[str2Words[i]] = 2
			end

			i += 1
		end

		print("\n\n Given str1 : ", str1)
		print("\n Given str2 : ", str2)
		print("\n Result : ")
		#  Display result
		record.each { | key, value |
			if (value != 3) 
				result = true
				print("  ", key)
			end

		}
		if (result == false) 
			print("\n None ")
		end

	end

end

def main() 
	task = Dissimilar.new()
	#  Test A
	str1 = "Test Mango orange pineapple and lemons"
	str2 = "Mango is sweet but lemons are sour Test"
	task.uncommonWords(str1, str2)
	#  Test B
	str1 = "Code One"
	str2 = "One Language"
	task.uncommonWords(str1, str2)
end

main()

Output


 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   orange  pineapple  and  is  sweet  but  are  sour

 Given str1 : Code One
 Given str2 : One Language
 Result :   Code  Language
import scala.collection.mutable._;
/*
    Scala program for
    Find uncommon words in two strings
*/
class Dissimilar()
{
	def uncommonWords(str1: String, str2: String): Unit = {
		var record: HashMap[String, Int] = 
          new HashMap[String, Int]();
		// Assume that string contains valid words
		var str1Words: Array[String] = str1.split(" ");
		var str2Words: Array[String] = str2.split(" ");
		var result: Boolean = false;
		var i: Int = 0;
		while (i < str1Words.length)
		{
			if (!record.contains(str1Words(i)))
			{
				// 1 indicates the word exist in first string
				record.addOne(str1Words(i), 1);
			}
			i += 1;
		}
		i = 0;
		while (i < str2Words.length)
		{
			if (record.contains(str2Words(i)) && 
                record.get(str2Words(i)).get == 1)
			{
				// 3 indicates the word exist in both string
				record.addOne(str2Words(i), 3);
			}
			else
			{
				// 2 indicates the word exist in second string
				record.addOne(str2Words(i), 2);
			}
			i += 1;
		}
		print("\n\n Given str1 : " + str1);
		print("\n Given str2 : " + str2);
		print("\n Result : ");
		// Display result
		for ((key, value) <- record)
		{
			if (value != 3)
			{
				result = true;
				print("  " + key);
			}
		}
		if (result == false)
		{
			print("\n None ");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Dissimilar = new Dissimilar();
		// Test A
		var str1: String = "Test Mango orange pineapple and lemons";
		var str2: String = "Mango is sweet but lemons are sour Test";
		task.uncommonWords(str1, str2);
		// Test B
		str1 = "Code One";
		str2 = "One Language";
		task.uncommonWords(str1, str2);
	}
}

Output

 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   but  is  sour  orange  pineapple  are  and  sweet

 Given str1 : Code One
 Given str2 : One Language
 Result :   Language  Code
import Foundation;
/*
    Swift 4 program for
    Find uncommon words in two strings
*/
class Dissimilar
{
	func uncommonWords(_ str1: String, _ str2: String)
	{
		var record: [String : Int] = [String: Int]();
		// Assume that string contains valid words
		let str1Words: [String] = str1.split
		{
			$0 == " "
		}.map(String.init);
		let str2Words: [String] = str2.split
		{
			$0 == " "
		}.map(String.init);
		var result: Bool = false;
		var i: Int = 0;
		while (i < str1Words.count)
		{
			if (!record.keys.contains(str1Words[i]))
			{
				// 1 indicates the word exist in first string
				record[str1Words[i]] = 1;
			}
			i += 1;
		}
		i = 0;
		while (i < str2Words.count)
		{
			if (record.keys.contains(str2Words[i]) && 
                record[str2Words[i]] == 1)
			{
				// 3 indicates the word exist in both string
				record[str2Words[i]] = 3;
			}
			else
			{
				// 2 indicates the word exist in second string
				record[str2Words[i]] = 2;
			}
			i += 1;
		}
		print("\n\n Given str1 : ", str1, terminator: "");
		print("\n Given str2 : ", str2, terminator: "");
		print("\n Result : ", terminator: "");
		// Display result
		for (key, value) in record
		{
			if (value != 3)
			{
				result = true;
				print("", key, terminator: "");
			}
		}
		if (result == false)
		{
			print("\n None ", terminator: "");
		}
	}
}
func main()
{
	let task: Dissimilar = Dissimilar();
	// Test A
	var str1: String = "Test Mango orange pineapple and lemons";
	var str2: String = "Mango is sweet but lemons are sour Test";
	task.uncommonWords(str1, str2);
	// Test B
	str1 = "Code One";
	str2 = "One Language";
	task.uncommonWords(str1, str2);
}
main();

Output

 Given str1 :  Test Mango orange pineapple and lemons
 Given str2 :  Mango is sweet but lemons are sour Test
 Result :  sweet and is but pineapple are orange sour

 Given str1 :  Code One
 Given str2 :  One Language
 Result :  Code Language
/*
    Kotlin program for
    Find uncommon words in two strings
*/
class Dissimilar
{
	fun uncommonWords(str1: String, str2: String): Unit
	{
		val record: HashMap < String, Int > = 
          HashMap < String, Int > ();
		// Assume that string contains valid words
		val str1Words: List < String > = str1.split(" ");
		val str2Words: List < String > = str2.split(" ");
		var result: Boolean = false;
		var i: Int = 0;
		while (i < str1Words.count())
		{
			if (!record.containsKey(str1Words[i]))
			{
				// 1 indicates the word exist in first string
				record.put(str1Words[i], 1);
			}
			i += 1;
		}
		i = 0;
		while (i < str2Words.count())
		{
			if (record.containsKey(str2Words[i]) && 
                record.getValue(str2Words[i]) == 1)
			{
				// 3 indicates the word exist in both string
				record.put(str2Words[i], 3);
			}
			else
			{
				// 2 indicates the word exist in second string
				record.put(str2Words[i], 2);
			}
			i += 1;
		}
		print("\n\n Given str1 : " + str1);
		print("\n Given str2 : " + str2);
		print("\n Result : ");
		// Display result
		for ((key, value) in record)
		{
			if (value != 3)
			{
				result = true;
				print("  " + key);
			}
		}
		if (result == false)
		{
			print("\n None ");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Dissimilar = Dissimilar();
	// Test A
	var str1: String = "Test Mango orange pineapple and lemons";
	var str2: String = "Mango is sweet but lemons are sour Test";
	task.uncommonWords(str1, str2);
	// Test B
	str1 = "Code One";
	str2 = "One Language";
	task.uncommonWords(str1, str2);
}

Output

 Given str1 : Test Mango orange pineapple and lemons
 Given str2 : Mango is sweet but lemons are sour Test
 Result :   orange  but  pineapple  are  and  is  sweet  sour

 Given str1 : Code One
 Given str2 : One Language
 Result :   Language  Code




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment