Skip to main content

Longest Uncommon Subsequence

Here given code implementation process.

/*
    Java program for
    Longest Uncommon Subsequence
*/
public class LUSubsequence
{
	public void findLUS(String str1, String str2)
	{
		int result = 0;
		if (str1.equals(str2) == false)
		{
			// When given string are not equal 
			if (str1.length() > str2.length())
			{
				// When str1 length is greater than str2 
				result = str1.length();
			}
			else
			{
				// When str2 length is greater than str1 
				result = str2.length();
			}
		}
		System.out.println(" Given str1 : " + str1);
		System.out.println(" Given str2 : " + str2);
		System.out.println(" Result : " + result);
	}
	public static void main(String[] args)
	{
		LUSubsequence task = new LUSubsequence();
		// Test A
		String str1 = "acb";
		String str2 = "cab";
		task.findLUS(str1, str2);
		// Test B
		str1 = "abc";
		str2 = "abc";
		task.findLUS(str1, str2);
		// Test C
		str1 = "abc";
		str2 = "fbabc";
		task.findLUS(str1, str2);
	}
}

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
// Include header file
#include <iostream>
#include <string>

using namespace std;
/*
    C++ program for
    Longest Uncommon Subsequence
*/
class LUSubsequence
{
	public: void findLUS(string str1, string str2)
	{
		int result = 0;
		if (str1.compare(str2) != 0 )
		{
			// When given string are not equal 
			if (str1.length() > str2.length())
			{
				// When str1 length is greater than str2 
				result = str1.length();
			}
			else
			{
				// When str2 length is greater than str1 
				result = str2.length();
			}
		}
		cout << " Given str1 : " << str1 << endl;
		cout << " Given str2 : " << str2 << endl;
		cout << " Result : " << result << endl;
	}
};
int main()
{
	LUSubsequence *task = new LUSubsequence();
	// Test A
	string str1 = "acb";
	string str2 = "cab";
	task->findLUS(str1, str2);
	// Test B
	str1 = "abc";
	str2 = "abc";
	task->findLUS(str1, str2);
	// Test C
	str1 = "abc";
	str2 = "fbabc";
	task->findLUS(str1, str2);
	return 0;
}

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
// Include namespace system
using System;
/*
    Csharp program for
    Longest Uncommon Subsequence
*/
public class LUSubsequence
{
	public void findLUS(String str1, String str2)
	{
		int result = 0;
		if (str1.Equals(str2) == false)
		{
			// When given string are not equal 
			if (str1.Length > str2.Length)
			{
				// When str1 length is greater than str2 
				result = str1.Length;
			}
			else
			{
				// When str2 length is greater than str1 
				result = str2.Length;
			}
		}
		Console.WriteLine(" Given str1 : " + str1);
		Console.WriteLine(" Given str2 : " + str2);
		Console.WriteLine(" Result : " + result);
	}
	public static void Main(String[] args)
	{
		LUSubsequence task = new LUSubsequence();
		// Test A
		String str1 = "acb";
		String str2 = "cab";
		task.findLUS(str1, str2);
		// Test B
		str1 = "abc";
		str2 = "abc";
		task.findLUS(str1, str2);
		// Test C
		str1 = "abc";
		str2 = "fbabc";
		task.findLUS(str1, str2);
	}
}

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
package main

import "fmt"
/*
    Go program for
    Longest Uncommon Subsequence
*/
func findLUS(str1, str2 string) {
	var result int = 0
	if str1 != str2 {
		// When given string are not equal 
		if len(str1) > len(str2) {
			// When str1 length is greater than str2 
			result = len(str1)
		} else {
			// When str2 length is greater than str1 
			result = len(str2)
		}
	}
	fmt.Println(" Given str1 : ", str1)
	fmt.Println(" Given str2 : ", str2)
	fmt.Println(" Result : ", result)
}
func main() {

	// Test A
	var str1 string = "acb"
	var str2 string = "cab"
	findLUS(str1, str2)
	// Test B
	str1 = "abc"
	str2 = "abc"
	findLUS(str1, str2)
	// Test C
	str1 = "abc"
	str2 = "fbabc"
	findLUS(str1, str2)
}

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
<?php
/*
    Php program for
    Longest Uncommon Subsequence
*/
class LUSubsequence
{
	public	function findLUS($str1, $str2)
	{
		$result = 0;
		if ((strcmp($str1, $str2) == 0) == false)
		{
			// When given string are not equal 
			if (strlen($str1) > strlen($str2))
			{
				// When str1 length is greater than str2 
				$result = strlen($str1);
			}
			else
			{
				// When str2 length is greater than str1 
				$result = strlen($str2);
			}
		}
		echo(" Given str1 : ".$str1.
			"\n");
		echo(" Given str2 : ".$str2.
			"\n");
		echo(" Result : ".$result.
			"\n");
	}
}

function main()
{
	$task = new LUSubsequence();
	// Test A
	$str1 = "acb";
	$str2 = "cab";
	$task->findLUS($str1, $str2);
	// Test B
	$str1 = "abc";
	$str2 = "abc";
	$task->findLUS($str1, $str2);
	// Test C
	$str1 = "abc";
	$str2 = "fbabc";
	$task->findLUS($str1, $str2);
}
main();

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
/*
    Node JS program for
    Longest Uncommon Subsequence
*/
class LUSubsequence
{
	findLUS(str1, str2)
	{
		var result = 0;
		if (str1.localeCompare(str2) != 0)
		{
			// When given string are not equal 
			if (str1.length > str2.length)
			{
				// When str1 length is greater than str2 
				result = str1.length;
			}
			else
			{
				// When str2 length is greater than str1 
				result = str2.length;
			}
		}
		console.log(" Given str1 : " + str1);
		console.log(" Given str2 : " + str2);
		console.log(" Result : " + result);
	}
}

function main()
{
	var task = new LUSubsequence();
	// Test A
	var str1 = "acb";
	var str2 = "cab";
	task.findLUS(str1, str2);
	// Test B
	str1 = "abc";
	str2 = "abc";
	task.findLUS(str1, str2);
	// Test C
	str1 = "abc";
	str2 = "fbabc";
	task.findLUS(str1, str2);
}
main();

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
#    Python 3 program for
#    Longest Uncommon Subsequence
class LUSubsequence :
	def findLUS(self, str1, str2) :
		result = 0
		if (str1 != str2) :
			#  When given string are not equal 
			if (len(str1) > len(str2)) :
				#  When str1 length is greater than str2 
				result = len(str1)
			else :
				#  When str2 length is greater than str1 
				result = len(str2)
			
		
		print(" Given str1 : ", str1)
		print(" Given str2 : ", str2)
		print(" Result : ", result)
	

def main() :
	task = LUSubsequence()
	#  Test A
	str1 = "acb"
	str2 = "cab"
	task.findLUS(str1, str2)
	#  Test B
	str1 = "abc"
	str2 = "abc"
	task.findLUS(str1, str2)
	#  Test C
	str1 = "abc"
	str2 = "fbabc"
	task.findLUS(str1, str2)

if __name__ == "__main__": main()

Output

 Given str1 :  acb
 Given str2 :  cab
 Result :  3
 Given str1 :  abc
 Given str2 :  abc
 Result :  0
 Given str1 :  abc
 Given str2 :  fbabc
 Result :  5
#    Ruby program for
#    Longest Uncommon Subsequence
class LUSubsequence 
	def findLUS(str1, str2) 
		result = 0
		if (str1 != str2) 
			#  When given string are not equal 
			if (str1.length > str2.length) 
				#  When str1 length is greater than str2 
				result = str1.length
			else
 
				#  When str2 length is greater than str1 
				result = str2.length
			end

		end

		print(" Given str1 : ", str1, "\n")
		print(" Given str2 : ", str2, "\n")
		print(" Result : ", result, "\n")
	end

end

def main() 
	task = LUSubsequence.new()
	#  Test A
	str1 = "acb"
	str2 = "cab"
	task.findLUS(str1, str2)
	#  Test B
	str1 = "abc"
	str2 = "abc"
	task.findLUS(str1, str2)
	#  Test C
	str1 = "abc"
	str2 = "fbabc"
	task.findLUS(str1, str2)
end

main()

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
import scala.collection.mutable._;
/*
    Scala program for
    Longest Uncommon Subsequence
*/
class LUSubsequence()
{
	def findLUS(str1: String, str2: String): Unit = {
		var result: Int = 0;
		if (str1.equals(str2) == false)
		{
			// When given string are not equal 
			if (str1.length() > str2.length())
			{
				// When str1 length is greater than str2 
				result = str1.length();
			}
			else
			{
				// When str2 length is greater than str1 
				result = str2.length();
			}
		}
		println(" Given str1 : " + str1);
		println(" Given str2 : " + str2);
		println(" Result : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: LUSubsequence = new LUSubsequence();
		// Test A
		var str1: String = "acb";
		var str2: String = "cab";
		task.findLUS(str1, str2);
		// Test B
		str1 = "abc";
		str2 = "abc";
		task.findLUS(str1, str2);
		// Test C
		str1 = "abc";
		str2 = "fbabc";
		task.findLUS(str1, str2);
	}
}

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5
import Foundation;
/*
    Swift 4 program for
    Longest Uncommon Subsequence
*/
class LUSubsequence
{
	func findLUS(_ str1: String, _ str2: String)
	{
		var result: Int = 0;
		if (str1 != str2)
		{
			// When given string are not equal 
			if (str1.count > str2.count)
			{
				// When str1 length is greater than str2 
				result = str1.count;
			}
			else
			{
				// When str2 length is greater than str1 
				result = str2.count;
			}
		}
		print(" Given str1 : ", str1);
		print(" Given str2 : ", str2);
		print(" Result : ", result);
	}
}
func main()
{
	let task: LUSubsequence = LUSubsequence();
	// Test A
	var str1: String = "acb";
	var str2: String = "cab";
	task.findLUS(str1, str2);
	// Test B
	str1 = "abc";
	str2 = "abc";
	task.findLUS(str1, str2);
	// Test C
	str1 = "abc";
	str2 = "fbabc";
	task.findLUS(str1, str2);
}
main();

Output

 Given str1 :  acb
 Given str2 :  cab
 Result :  3
 Given str1 :  abc
 Given str2 :  abc
 Result :  0
 Given str1 :  abc
 Given str2 :  fbabc
 Result :  5
/*
    Kotlin program for
    Longest Uncommon Subsequence
*/
class LUSubsequence
{
	fun findLUS(str1: String, str2: String): Unit
	{
		var result: Int = 0;
		if (str1.equals(str2) == false)
		{
			// When given string are not equal 
			if (str1.length > str2.length)
			{
				// When str1 length is greater than str2 
				result = str1.length;
			}
			else
			{
				// When str2 length is greater than str1 
				result = str2.length;
			}
		}
		println(" Given str1 : " + str1);
		println(" Given str2 : " + str2);
		println(" Result : " + result);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: LUSubsequence = LUSubsequence();
	// Test A
	var str1: String = "acb";
	var str2: String = "cab";
	task.findLUS(str1, str2);
	// Test B
	str1 = "abc";
	str2 = "abc";
	task.findLUS(str1, str2);
	// Test C
	str1 = "abc";
	str2 = "fbabc";
	task.findLUS(str1, str2);
}

Output

 Given str1 : acb
 Given str2 : cab
 Result : 3
 Given str1 : abc
 Given str2 : abc
 Result : 0
 Given str1 : abc
 Given str2 : fbabc
 Result : 5




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