Longest subsequence with different adjacent characters

Here given code implementation process.

// Java program for
// Longest subsequence with different adjacent characters
public class Subsequence
{
	public void longestDiffAdjacent(String text)
	{
		int count = 1;
		if (text.length() == 0)
		{
			count = 0;
		}
		else
		{
			char back = text.charAt(0);
			// iterating the text element
			for (int i = 1; i < text.length(); ++i)
			{
				if (text.charAt(i) != back)
				{
					count++;
					// Update the back with current character
					back = text.charAt(i);
				}
			}
		}
		// Display the value of given text 
		System.out.println(" Given Text : " + text);
		// Display calculated result
		System.out.println(" Longest different adjacent  : " + count);
	}
	public static void main(String[] args)
	{
		Subsequence task = new Subsequence();
		// Test cases
		task.longestDiffAdjacent("abbccce");
		task.longestDiffAdjacent("aaaaaaa");
		task.longestDiffAdjacent("aaaabeeabb");
		task.longestDiffAdjacent("xyxyyxy");
	}
}

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6
// Include header file
#include <iostream>
#include <string>
using namespace std;

// C++ program for
// Longest subsequence with different adjacent characters

class Subsequence
{
	public: void longestDiffAdjacent(string text)
	{
		int count = 1;
		if (text.length() == 0)
		{
			count = 0;
		}
		else
		{
			char back = text[0];
			// iterating the text element
			for (int i = 1; i < text.length(); ++i)
			{
				if (text[i] != back)
				{
					count++;
					// Update the back with current character
					back = text[i];
				}
			}
		}
		// Display the value of given text 
		cout << " Given Text : " << text << endl;
		// Display calculated result
		cout << " Longest different adjacent  : " << count << endl;
	}
};
int main()
{
	Subsequence *task = new Subsequence();
	// Test cases
	task->longestDiffAdjacent("abbccce");
	task->longestDiffAdjacent("aaaaaaa");
	task->longestDiffAdjacent("aaaabeeabb");
	task->longestDiffAdjacent("xyxyyxy");
	return 0;
}

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6
// Include namespace system
using System;
// Csharp program for
// Longest subsequence with different adjacent characters
public class Subsequence
{
	public void longestDiffAdjacent(String text)
	{
		int count = 1;
		if (text.Length == 0)
		{
			count = 0;
		}
		else
		{
			char back = text[0];
			// iterating the text element
			for (int i = 1; i < text.Length; ++i)
			{
				if (text[i] != back)
				{
					count++;
					// Update the back with current character
					back = text[i];
				}
			}
		}
		// Display the value of given text 
		Console.WriteLine(" Given Text : " + text);
		// Display calculated result
		Console.WriteLine(" Longest different adjacent  : " + count);
	}
	public static void Main(String[] args)
	{
		Subsequence task = new Subsequence();
		// Test cases
		task.longestDiffAdjacent("abbccce");
		task.longestDiffAdjacent("aaaaaaa");
		task.longestDiffAdjacent("aaaabeeabb");
		task.longestDiffAdjacent("xyxyyxy");
	}
}

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6
<?php
// Php program for
// Longest subsequence with different adjacent characters
class Subsequence
{
	public	function longestDiffAdjacent($text)
	{
		$count = 1;
		if (strlen($text) == 0)
		{
			$count = 0;
		}
		else
		{
			$back = $text[0];
			// iterating the text element
			for ($i = 1; $i < strlen($text); ++$i)
			{
				if ($text[$i] != $back)
				{
					$count++;
					// Update the back with current character
					$back = $text[$i];
				}
			}
		}
		// Display the value of given text 
		echo(" Given Text : ".$text.
			"\n");
		// Display calculated result
		echo(" Longest different adjacent  : ".$count.
			"\n");
	}
}

function main()
{
	$task = new Subsequence();
	// Test cases
	$task->longestDiffAdjacent("abbccce");
	$task->longestDiffAdjacent("aaaaaaa");
	$task->longestDiffAdjacent("aaaabeeabb");
	$task->longestDiffAdjacent("xyxyyxy");
}
main();

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6
// Node JS program for
// Longest subsequence with different adjacent characters
class Subsequence
{
	longestDiffAdjacent(text)
	{
		var count = 1;
		if (text.length == 0)
		{
			count = 0;
		}
		else
		{
			var back = text.charAt(0);
			// iterating the text element
			for (var i = 1; i < text.length; ++i)
			{
				if (text.charAt(i) != back)
				{
					count++;
					// Update the back with current character
					back = text.charAt(i);
				}
			}
		}
		// Display the value of given text 
		console.log(" Given Text : " + text);
		// Display calculated result
		console.log(" Longest different adjacent  : " + count);
	}
}

function main()
{
	var task = new Subsequence();
	// Test cases
	task.longestDiffAdjacent("abbccce");
	task.longestDiffAdjacent("aaaaaaa");
	task.longestDiffAdjacent("aaaabeeabb");
	task.longestDiffAdjacent("xyxyyxy");
}
main();

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6
#  Python 3 program for
#  Longest subsequence with different adjacent characters
class Subsequence :
	def longestDiffAdjacent(self, text) :
		count = 1
		if (len(text) == 0) :
			count = 0
		else :
			back = text[0]
			#  iterating the text element
			i = 1
			while (i < len(text)) :
				if (text[i] != back) :
					count += 1
					#  Update the back with current character
					back = text[i]
				
				i += 1
			
		
		#  Display the value of given text 
		print(" Given Text : ", text)
		#  Display calculated result
		print(" Longest different adjacent  : ", count)
	

def main() :
	task = Subsequence()
	#  Test cases
	task.longestDiffAdjacent("abbccce")
	task.longestDiffAdjacent("aaaaaaa")
	task.longestDiffAdjacent("aaaabeeabb")
	task.longestDiffAdjacent("xyxyyxy")

if __name__ == "__main__": main()

input

 Given Text :  abbccce
 Longest different adjacent  :  4
 Given Text :  aaaaaaa
 Longest different adjacent  :  1
 Given Text :  aaaabeeabb
 Longest different adjacent  :  5
 Given Text :  xyxyyxy
 Longest different adjacent  :  6
#  Ruby program for
#  Longest subsequence with different adjacent characters
class Subsequence 
	def longestDiffAdjacent(text) 
		count = 1
		if (text.length == 0) 
			count = 0
		else
 
			back = text[0]
			#  iterating the text element
			i = 1
			while (i < text.length) 
				if (text[i] != back) 
					count += 1
					#  Update the back with current character
					back = text[i]
				end

				i += 1
			end

		end

		#  Display the value of given text 
		print(" Given Text : ", text, "\n")
		#  Display calculated result
		print(" Longest different adjacent  : ", count, "\n")
	end

end

def main() 
	task = Subsequence.new()
	#  Test cases
	task.longestDiffAdjacent("abbccce")
	task.longestDiffAdjacent("aaaaaaa")
	task.longestDiffAdjacent("aaaabeeabb")
	task.longestDiffAdjacent("xyxyyxy")
end

main()

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6
import scala.collection.mutable._;
// Scala program for
// Longest subsequence with different adjacent characters
class Subsequence()
{
	def longestDiffAdjacent(text: String): Unit = {
		var count: Int = 1;
		if (text.length() == 0)
		{
			count = 0;
		}
		else
		{
			var back: Char = text.charAt(0);
			// iterating the text element
			var i: Int = 1;
			while (i < text.length())
			{
				if (text.charAt(i) != back)
				{
					count += 1;
					// Update the back with current character
					back = text.charAt(i);
				}
				i += 1;
			}
		}
		// Display the value of given text 
		println(" Given Text : " + text);
		// Display calculated result
		println(" Longest different adjacent  : " + count);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Subsequence = new Subsequence();
		// Test cases
		task.longestDiffAdjacent("abbccce");
		task.longestDiffAdjacent("aaaaaaa");
		task.longestDiffAdjacent("aaaabeeabb");
		task.longestDiffAdjacent("xyxyyxy");
	}
}

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6
import Foundation;
// Swift 4 program for
// Longest subsequence with different adjacent characters
class Subsequence
{
	func longestDiffAdjacent(_ data: String)
	{
		var count = 1;
      	let text = Array(data);
		if (text.count == 0)
		{
			count = 0;
		}
		else
		{
			var back = text[0];
			// iterating the text element
			var i = 1;
			while (i < text.count)
			{
				if (text[i]  != back)
				{
					count += 1;
					// Update the back with current character
					back = text[i];
				}
				i += 1;
			}
		}
		// Display the value of given text 
		print(" Given Text : ", data);
		// Display calculated result
		print(" Longest different adjacent  : ", count);
	}
}
func main()
{
	let task = Subsequence();
	// Test cases
	task.longestDiffAdjacent("abbccce");
	task.longestDiffAdjacent("aaaaaaa");
	task.longestDiffAdjacent("aaaabeeabb");
	task.longestDiffAdjacent("xyxyyxy");
}
main();

input

 Given Text :  abbccce
 Longest different adjacent  :  4
 Given Text :  aaaaaaa
 Longest different adjacent  :  1
 Given Text :  aaaabeeabb
 Longest different adjacent  :  5
 Given Text :  xyxyyxy
 Longest different adjacent  :  6
// Kotlin program for
// Longest subsequence with different adjacent characters
class Subsequence
{
	fun longestDiffAdjacent(text: String): Unit
	{
		var count: Int = 1;
		if (text.length == 0)
		{
			count = 0;
		}
		else
		{
			var back: Char = text.get(0);
			// iterating the text element
			var i: Int = 1;
			while (i < text.length)
			{
				if (text.get(i) != back)
				{
					count += 1;
					// Update the back with current character
					back = text.get(i);
				}
				i += 1;
			}
		}
		// Display the value of given text 
		println(" Given Text : " + text);
		// Display calculated result
		println(" Longest different adjacent  : " + count);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Subsequence = Subsequence();
	// Test cases
	task.longestDiffAdjacent("abbccce");
	task.longestDiffAdjacent("aaaaaaa");
	task.longestDiffAdjacent("aaaabeeabb");
	task.longestDiffAdjacent("xyxyyxy");
}

input

 Given Text : abbccce
 Longest different adjacent  : 4
 Given Text : aaaaaaa
 Longest different adjacent  : 1
 Given Text : aaaabeeabb
 Longest different adjacent  : 5
 Given Text : xyxyyxy
 Longest different adjacent  : 6


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







© 2021, kalkicode.com, All rights reserved