Length of the longest substring without repeating characters

Here given code implementation process.

// Java Program 
// Length of the longest substring without repeating characters
public class Substring
{
  	// Returns a max value of given two numbers
	public int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void longestUniqueString(String text)
	{
		int n = text.length();
		int count = 0;
		int auxiliary = 0;
		int k = 0;
		// Use to collect position 
		int[] index = new int[256];
		// Set initial value
		for (int i = 0; i < 256; ++i)
		{
			index[i] = -1;
		}
		for (int i = 0; i < n; ++i)
		{
			k = maxValue(index[text.charAt(i)] + 1, k);
			index[text.charAt(i)] = i;
			count = maxValue(i - k + 1, count);
		}
		// Display given text
		System.out.println("Given text : " + text);
		System.out.println("Longest non repeating character : " + count);
	}
	public static void main(String[] args)
	{
		Substring task = new Substring();
		// "licos" is longest distinct character
		// Ans : 5
		task.longestUniqueString("silicosis");
		// "xyapin" is longest distinct character
		// Ans : 6
		task.longestUniqueString("abcaxyapinxinig");
	}
}

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
// Include header file
#include <iostream>
#include <string>
using namespace std;
// C++ Program 
// Length of the longest substring without repeating characters
class Substring
{
	public:
		// Returns a max value of given two numbers
		int maxValue(int a, int b)
		{
			if (a > b)
			{
				return a;
			}
			return b;
		}
	void longestUniqueString(string text)
	{
		int n = text.length();
		int count = 0;
		int auxiliary = 0;
		int k = 0;
		// Use to collect position 
		int index[256];
		// Set initial value
		for (int i = 0; i < 256; ++i)
		{
			index[i] = -1;
		}
		for (int i = 0; i < n; ++i)
		{
			k = this->maxValue(index[text[i]] + 1, k);
			index[text[i]] = i;
			count = this->maxValue(i - k + 1, count);
		}
		// Display given text
		cout << "Given text : " << text << endl;
		cout << "Longest non repeating character : " << count << endl;
	}
};
int main()
{
	Substring *task = new Substring();
	// "licos" is longest distinct character
	// Ans : 5
	task->longestUniqueString("silicosis");
	// "xyapin" is longest distinct character
	// Ans : 6
	task->longestUniqueString("abcaxyapinxinig");
	return 0;
}

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
// Include namespace system
using System;
// Csharp Program 
// Length of the longest substring without repeating characters
public class Substring
{
	// Returns a max value of given two numbers
	public int maxValue(int a, int b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	public void longestUniqueString(String text)
	{
		int n = text.Length;
		int count = 0;
		int k = 0;
		// Use to collect position 
		int[] index = new int[256];
		// Set initial value
		for (int i = 0; i < 256; ++i)
		{
			index[i] = -1;
		}
		for (int i = 0; i < n; ++i)
		{
			k = this.maxValue(index[text[i]] + 1, k);
			index[text[i]] = i;
			count = this.maxValue(i - k + 1, count);
		}
		// Display given text
		Console.WriteLine("Given text : " + text);
		Console.WriteLine("Longest non repeating character : " + count);
	}
	public static void Main(String[] args)
	{
		Substring task = new Substring();
		// "licos" is longest distinct character
		// Ans : 5
		task.longestUniqueString("silicosis");
		// "xyapin" is longest distinct character
		// Ans : 6
		task.longestUniqueString("abcaxyapinxinig");
	}
}

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
package main
import "fmt"
// Go Program 
// Length of the longest substring without repeating characters

// Returns a max value of given two numbers
func maxValue(a, b int) int {
	if a > b {
		return a
	}
	return b
}
func longestUniqueString(text string) {
	var n int = len(text)
	var count int = 0
	var k int = 0
	// Use to collect position 
	// Set initial value
	var index = make([]int,256)

	for i := 0; i < 256; i++ {
		index[i] = -1
	}
	for i := 0 ; i < n ; i++ {
		k = maxValue(index[text[i] + 1] + 1, k)
		index[text[i] + 1] = i
		count = maxValue(i - k + 1, count)
	}
	// Display given text
	fmt.Println("Given text : ", text)
	fmt.Println("Longest non repeating character : ", count)
}
func main() {
	
	// "licos" is longest distinct character
	// Ans : 5
	longestUniqueString("silicosis")
	// "xyapin" is longest distinct character
	// Ans : 6
	longestUniqueString("abcaxyapinxinig")
}

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
<?php
// Php Program 
// Length of the longest substring without repeating characters
class Substring
{
	// Returns a max value of given two numbers
	public	function maxValue($a, $b)
	{
		if ($a > $b)
		{
			return $a;
		}
		return $b;
	}
	public	function longestUniqueString($text)
	{
		$n = strlen($text);
		$count = 0;
		$k = 0;
		// Use to collect position 
		// Set initial value
		$index = array_fill(0, 256, -1);
		for ($i = 0; $i < $n; ++$i)
		{
			$k = $this->maxValue($index[ord($text[$i])] + 1, $k);
			$index[ord($text[$i])] = $i;
			$count = $this->maxValue($i - $k + 1, $count);
		}
		// Display given text
		echo("Given text : ".$text.
			"\n");
		echo("Longest non repeating character : ".$count.
			"\n");
	}
}

function main()
{
	$task = new Substring();
	// "licos" is longest distinct character
	// Ans : 5
	$task->longestUniqueString("silicosis");
	// "xyapin" is longest distinct character
	// Ans : 6
	$task->longestUniqueString("abcaxyapinxinig");
}
main();

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
// Node JS Program 
// Length of the longest substring without repeating characters
class Substring
{
	// Returns a max value of given two numbers
	maxValue(a, b)
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	longestUniqueString(text)
	{
		var n = text.length;
		var count = 0;
		var k = 0;
		// Use to collect position 
		// Set initial value
		var index = Array(256).fill(0);
		for (var i = 0; i < n; ++i)
		{
			k = this.maxValue(index[text.charCodeAt(i)] + 1, k);
			index[text.charCodeAt(i)] = i;
			count = this.maxValue(i - k + 1, count);
		}
		// Display given text
		console.log("Given text : " + text);
		console.log("Longest non repeating character : " + count);
	}
}

function main()
{
	var task = new Substring();
	// "licos" is longest distinct character
	// Ans : 5
	task.longestUniqueString("silicosis");
	// "xyapin" is longest distinct character
	// Ans : 6
	task.longestUniqueString("abcaxyapinxinig");
}
main();

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
#  Python 3 Program 
#  Length of the longest substring without repeating characters
class Substring :
	#  Returns a max value of given two numbers
	def maxValue(self, a, b) :
		if (a > b) :
			return a
		
		return b
	
	def longestUniqueString(self, text) :
		n = len(text)
		count = 0
		k = 0
		#  Use to collect position 
		#  Set initial value
		index = [-1] * (256)
		i = 0
		while (i < n) :
			k = self.maxValue(index[ord(text[i])] + 1, k)
			index[ord(text[i])] = i
			count = self.maxValue(i - k + 1, count)
			i += 1
		
		#  Display given text
		print("Given text : ", text)
		print("Longest non repeating character : ", count)
	

def main() :
	task = Substring()
	#  "licos" is longest distinct character
	#  Ans : 5
	task.longestUniqueString("silicosis")
	#  "xyapin" is longest distinct character
	#  Ans : 6
	task.longestUniqueString("abcaxyapinxinig")

if __name__ == "__main__": main()

Output

Given text :  silicosis
Longest non repeating character :  5
Given text :  abcaxyapinxinig
Longest non repeating character :  6
#  Ruby Program 
#  Length of the longest substring without repeating characters
class Substring 
	#  Returns a max value of given two numbers
	def maxValue(a, b) 
		if (a > b) 
			return a
		end

		return b
	end

	def longestUniqueString(text) 
		n = text.length
		count = 0
		k = 0
		#  Use to collect position 
		#  Set initial value
		index = Array.new(256) {-1}
		i = 0
		while (i < n) 
			k = self.maxValue(index[text[i].ord] + 1, k)
			index[text[i].ord] = i
			count = self.maxValue(i - k + 1, count)
			i += 1
		end

		#  Display given text
		print("Given text : ", text, "\n")
		print("Longest non repeating character : ", count, "\n")
	end

end

def main() 
	task = Substring.new()
	#  "licos" is longest distinct character
	#  Ans : 5
	task.longestUniqueString("silicosis")
	#  "xyapin" is longest distinct character
	#  Ans : 6
	task.longestUniqueString("abcaxyapinxinig")
end

main()

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
// Scala Program 
// Length of the longest substring without repeating characters
class Substring()
{
	// Returns a max value of given two numbers
	def maxValue(a: Int, b: Int): Int = {
		if (a > b)
		{
			return a;
		}
		return b;
	}
	def longestUniqueString(text: String): Unit = {
		var n: Int = text.length();
		var count: Int = 0;
		var k: Int = 0;
		// Use to collect position 
		// Set initial value
		var index: Array[Int] = Array.fill[Int](256)(-1);
		var i: Int = 0;
		while (i < n)
		{
			k = maxValue(index(text.charAt(i).toInt + 1) + 1, k);
			index(text.charAt(i).toInt + 1) = i;
			count = maxValue(i - k + 1, count);
			i += 1;
		}
		// Display given text
		println("Given text : " + text);
		println("Longest non repeating character : " + count);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Substring = new Substring();
		// "licos" is longest distinct character
		// Ans : 5
		task.longestUniqueString("silicosis");
		// "xyapin" is longest distinct character
		// Ans : 6
		task.longestUniqueString("abcaxyapinxinig");
	}
}

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 6
import Foundation;
// Swift 4 Program 
// Length of the longest substring without repeating characters
class Substring
{
	// Returns a max value of given two numbers
	func maxValue(_ a: Int, _ b: Int) -> Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	func longestUniqueString(_ data: String)
	{
		let n: Int = data.count;
		var count: Int = 0;
		var k: Int = 0;
      	let text = Array(data);
		// Use to collect position 
		// Set initial value
		var index: [Int] = Array(repeating: -1, count: 256);
		var i: Int = 0;
		while (i < n)
		{
			k = self.maxValue(index[Int(UnicodeScalar(String(text[i]))!.value)] + 1, k);
			index[Int(UnicodeScalar(String(text[i]))!.value)] = i;
			count = self.maxValue(i - k + 1, count);
			i += 1;
		}
		// Display given text
		print("Given text : ", data);
		print("Longest non repeating character : ", count);
	}
}
func main()
{
	let task: Substring = Substring();
	// "licos" is longest distinct character
	// Ans : 5
	task.longestUniqueString("silicosis");
	// "xyapin" is longest distinct character
	// Ans : 6
	task.longestUniqueString("abcaxyapinxinig");
}
main();

Output

Given text :  silicosis
Longest non repeating character :  5
Given text :  abcaxyapinxinig
Longest non repeating character :  6
// Kotlin Program 
// Length of the longest substring without repeating characters
class Substring
{
	// Returns a max value of given two numbers
	fun maxValue(a: Int, b: Int): Int
	{
		if (a > b)
		{
			return a;
		}
		return b;
	}
	fun longestUniqueString(text: String): Unit
	{
		val n: Int = text.length;
		var count: Int = 0;
		var k: Int = 0;
		// Use to collect position 
		// Set initial value
		val index: Array < Int > = Array(256)
		{
			-1
		};
		var i: Int = 0;
		while (i < n)
		{
			k = this.maxValue(index[text.get(i).toInt()] + 1, k);
			index[text.get(i).toInt()] = i;
			count = this.maxValue(i - k + 1, count);
			i += 1;
		}
		// Display given text
		println("Given text : " + text);
		println("Longest non repeating character : " + count);
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Substring = Substring();
	// "licos" is longest distinct character
	// Ans : 5
	task.longestUniqueString("silicosis");
	// "xyapin" is longest distinct character
	// Ans : 6
	task.longestUniqueString("abcaxyapinxinig");
}

Output

Given text : silicosis
Longest non repeating character : 5
Given text : abcaxyapinxinig
Longest non repeating character : 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