Find longest substring of unique characters
Here given code implementation process.
//C Program
//Find longest substring of unique characters
#include <stdio.h>
//find the longest unique substring of a string
void longest_unique_substring(char * text, int size)
{
if (size <= 1)
{
return;
}
int length = -1, x = 0, front = 0;
//Loop controlling variable
int i = 1, j = 0;
//Loop iterating the string elements
for (i = 1; i < size; ++i)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
for (j = x; j < i; j++)
{
if (text[j] == text[i])
{
//When character exists
break;
}
}
if (j == i)
{
//When get a new substring
if ((j - x) > length)
{
// When get a newly longest unique substring
// Get length
length = j - x;
// Get starting location
front = x;
}
}
else
{
x = x + 1;
i = x;
}
}
printf(" Given String : %s\n", text);
printf(" Longest Unique Substring : ");
//Note that at least one unique character always exist in string
for (i = front; i <= front + length; ++i)
{
printf("%c", text[i]);
}
printf("\n");
}
int main()
{
//Define the character of array elements
char text[] = "ABCABD";
//Get the size
int size = sizeof(text) / sizeof(text[0]) - 1;
//Find result
longest_unique_substring(text, size);
//Other test cases
char text1[] = "FBCSBSNL";
size = sizeof(text1) / sizeof(text1[0]) - 1;
longest_unique_substring(text1, size);
char text2[] = "AABFBCAEBO";
size = sizeof(text2) / sizeof(text2[0]) - 1;
longest_unique_substring(text2, size);
char text3[] = "AABAAAB";
size = sizeof(text3) / sizeof(text3[0]) - 1;
longest_unique_substring(text3, size);
char text4[] = "BAACCAAB";
size = sizeof(text4) / sizeof(text4[0]) - 1;
longest_unique_substring(text4, size);
return 0;
}
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
//Java Program
//Find longest substring of unique characters
class MyString
{
//Find the longest unique substring of a string
public void longest_unique_substring(String text)
{
int size = text.length();
if (size <= 1)
{
return;
}
int length = -1, x = 0, front = 0;
//Loop controlling variable
int i = 1, j = 0;
//Loop iterating the string elements
for (i = 1; i < size; ++i)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
for (j = x; j < i; j++)
{
if (text.charAt(j) == text.charAt(i))
{
break;
}
}
if (j == i)
{
//When get a new substring
if ((j - x) > length)
{
// When get a newly longest unique substring
// Get length
length = j - x;
// Get starting location
front = x;
}
}
else
{
x = x + 1;
i = x;
}
}
System.out.print(" Given String : " + text + "\n");
System.out.print(" Longest Unique Substring : ");
//Note that at least one unique character always exist in string
for (i = front; i <= front + length; ++i)
{
System.out.print(text.charAt(i));
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyString obj = new MyString();
obj.longest_unique_substring("ABCABD");
obj.longest_unique_substring("FBCSBSNL");
obj.longest_unique_substring("AABFBCAEBO");
obj.longest_unique_substring("AABAAAB");
obj.longest_unique_substring("BAACCAAB");
}
}
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
//Include header file
#include <iostream>
#include<string.h>
using namespace std;
//C++ Program
//Find longest substring of unique characters
class MyString
{
public:
//Find the longest unique substring of a string
void longest_unique_substring(string text)
{
int size = text.size();
if (size <= 1)
{
return;
}
int length = -1, x = 0, front = 0;
//Loop controlling variable
int i = 1, j = 0;
//Loop iterating the string elements
for (i = 1; i < size; ++i)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
for (j = x; j < i; j++)
{
if (text[j] == text[i])
{
break;
}
}
if (j == i)
{
//When get a new substring
if ((j - x) > length)
{
// When get a newly longest unique substring
// Get length
length = j - x;
// Get starting location
front = x;
}
}
else
{
x = x + 1;
i = x;
}
}
cout << " Given String : " << text << "\n";
cout << " Longest Unique Substring : ";
//Note that at least one unique character always exist in string
for (i = front; i <= front + length; ++i)
{
cout << text[i];
}
cout << "\n";
}
};
int main()
{
MyString obj = MyString();
obj.longest_unique_substring("ABCABD");
obj.longest_unique_substring("FBCSBSNL");
obj.longest_unique_substring("AABFBCAEBO");
obj.longest_unique_substring("AABAAAB");
obj.longest_unique_substring("BAACCAAB");
return 0;
}
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
//Include namespace system
using System;
//C# Program
//Find longest substring of unique characters
class MyString
{
//Find the longest unique substring of a string
public void longest_unique_substring(String text)
{
int size = text.Length;
if (size <= 1)
{
return;
}
int length = -1, x = 0, front = 0;
//Loop controlling variable
int i = 1, j = 0;
//Loop iterating the string elements
for (i = 1; i < size; ++i)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
for (j = x; j < i; j++)
{
if (text[j] == text[i])
{
break;
}
}
if (j == i)
{
//When get a new substring
if ((j - x) > length)
{
// When get a newly longest unique substring
// Get length
length = j - x;
// Get starting location
front = x;
}
}
else
{
x = x + 1;
i = x;
}
}
Console.Write(" Given String : " + text + "\n");
Console.Write(" Longest Unique Substring : ");
//Note that at least one unique character always exist in string
for (i = front; i <= front + length; ++i)
{
Console.Write(text[i]);
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyString obj = new MyString();
obj.longest_unique_substring("ABCABD");
obj.longest_unique_substring("FBCSBSNL");
obj.longest_unique_substring("AABFBCAEBO");
obj.longest_unique_substring("AABAAAB");
obj.longest_unique_substring("BAACCAAB");
}
}
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
<?php
//Php Program
//Find longest substring of unique characters
class MyString
{
//Find the longest unique substring of a string
public function longest_unique_substring($text)
{
$size = strlen($text);
if ($size <= 1)
{
return;
}
$length = -1;
$x = 0;
$front = 0;
//Loop controlling variable
$i = 1;
$j = 0;
//Loop iterating the string elements
for ($i = 1; $i < $size; ++$i)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
for ($j = $x; $j < $i; $j++)
{
if ($text[$j] == $text[$i])
{
break;
}
}
if ($j == $i)
{
//When get a new substring
if (($j - $x) > $length)
{
// When get a newly longest unique substring
// Get length
$length = $j - $x;
// Get starting location
$front = $x;
}
}
else
{
$x = $x + 1;
$i = $x;
}
}
echo " Given String : ". $text ."\n";
echo " Longest Unique Substring : ";
//Note that at least one unique character always exist in string
for ($i = $front; $i <= $front + $length; ++$i)
{
echo $text[$i];
}
echo "\n";
}
}
function main()
{
$obj = new MyString();
$obj->longest_unique_substring("ABCABD");
$obj->longest_unique_substring("FBCSBSNL");
$obj->longest_unique_substring("AABFBCAEBO");
$obj->longest_unique_substring("AABAAAB");
$obj->longest_unique_substring("BAACCAAB");
}
main();
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
//Node Js Program
//Find longest substring of unique characters
class MyString
{
//Find the longest unique substring of a string
longest_unique_substring(text)
{
var size = text.length;
if (size <= 1)
{
return;
}
var length = -1;
var x = 0;
var front = 0;
//Loop controlling variable
var i = 1;
var j = 0;
//Loop iterating the string elements
for (i = 1; i < size; ++i)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
for (j = x; j < i; j++)
{
if (text[j] == text[i])
{
break;
}
}
if (j == i)
{
//When get a new substring
if ((j - x) > length)
{
// When get a newly longest unique substring
// Get length
length = j - x;
// Get starting location
front = x;
}
}
else
{
x = x + 1;
i = x;
}
}
process.stdout.write(" Given String : " + text + "\n");
process.stdout.write(" Longest Unique Substring : ");
//Note that at least one unique character always exist in string
for (i = front; i <= front + length; ++i)
{
process.stdout.write(text[i]);
}
process.stdout.write("\n");
}
}
function main()
{
var obj = new MyString();
obj.longest_unique_substring("ABCABD");
obj.longest_unique_substring("FBCSBSNL");
obj.longest_unique_substring("AABFBCAEBO");
obj.longest_unique_substring("AABAAAB");
obj.longest_unique_substring("BAACCAAB");
}
main();
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
# Python 3 Program
# Find longest substring of unique characters
class MyString :
# Find the longest unique substring of a string
def longest_unique_substring(self, text) :
size = len(text)
if (size <= 1) :
return
length = -1
x = 0
front = 0
# Loop controlling variable
i = 1
j = 0
# Loop iterating the string elements
while (i < size) :
# check that whether [i] location character exists
# with between of [x to i] location or not
j = x
while (j < i) :
if (text[j] == text[i]) :
break
j += 1
if (j == i) :
# When get a new substring
if ((j - x) > length) :
# When get a newly longest unique substring
# Get length
length = j - x
# Get starting location
front = x
else :
x = x + 1
i = x
i += 1
print(" Given String : ", text ,"\n", end = "")
print(" Longest Unique Substring : ", end = "")
# Note that at least one unique character always exist in string
i = front
while (i <= front + length) :
print(text[i], end = "")
i += 1
print("\n", end = "")
def main() :
obj = MyString()
obj.longest_unique_substring("ABCABD")
obj.longest_unique_substring("FBCSBSNL")
obj.longest_unique_substring("AABFBCAEBO")
obj.longest_unique_substring("AABAAAB")
obj.longest_unique_substring("BAACCAAB")
if __name__ == "__main__": main()
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
# Ruby Program
# Find longest substring of unique characters
class MyString
# Find the longest unique substring of a string
def longest_unique_substring(text)
size = text.length()
if (size <= 1)
return
end
length = -1
x = 0
front = 0
# Loop controlling variable
i = 1
j = 0
# Loop iterating the string elements
while (i < size)
# check that whether [i] location character exists
# with between of [x to i] location or not
j = x
while (j < i)
if (text[j] == text[i])
break
end
j += 1
end
if (j == i)
# When get a new substring
if ((j - x) > length)
# When get a newly longest unique substring
# Get length
length = j - x
# Get starting location
front = x
end
else
x = x + 1
i = x
end
i += 1
end
print(" Given String : ", text ,"\n")
print(" Longest Unique Substring : ")
# Note that at least one unique character always exist in string
i = front
while (i <= front + length)
print(text[i])
i += 1
end
print("\n")
end
end
def main()
obj = MyString.new()
obj.longest_unique_substring("ABCABD")
obj.longest_unique_substring("FBCSBSNL")
obj.longest_unique_substring("AABFBCAEBO")
obj.longest_unique_substring("AABAAAB")
obj.longest_unique_substring("BAACCAAB")
end
main()
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
import scala.util.control.Breaks._
//Scala Program
//Find longest substring of unique characters
class MyString
{
//Find the longest unique substring of a string
def longest_unique_substring(text: String): Unit = {
var size: Int = text.length();
if (size <= 1)
{
return;
}
var length: Int = -1;
var x: Int = 0;
var front: Int = 0;
//Loop controlling variable
var i: Int = 1;
var j: Int = 0;
//Loop iterating the string elements
while (i < size)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
j = x;
breakable {
while (j < i)
{
if (text(j) == text(i))
{
break;
}
j += 1;
}
}
if (j == i)
{
//When get a new substring
if ((j - x) > length)
{
// When get a newly longest unique substring
// Get length
length = j - x;
// Get starting location
front = x;
}
}
else
{
x = x + 1;
i = x;
}
i += 1;
}
print(" Given String : " + text + "\n");
print(" Longest Unique Substring : ");
//Note that at least one unique character always exist in string
i = front;
while (i <= front + length)
{
print(text(i));
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
obj.longest_unique_substring("ABCABD");
obj.longest_unique_substring("FBCSBSNL");
obj.longest_unique_substring("AABFBCAEBO");
obj.longest_unique_substring("AABAAAB");
obj.longest_unique_substring("BAACCAAB");
}
}
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
//Swift Program
//Find longest substring of unique characters
class MyString
{
//Find the longest unique substring of a string
func longest_unique_substring(_ str: String)
{
let size: Int = str.count;
if (size <= 1)
{
return;
}
let text = Array(str);
var length: Int = -1;
var x: Int = 0;
var front: Int = 0;
//Loop controlling variable
var i: Int = 1;
var j: Int = 0;
//Loop iterating the string elements
while (i < size)
{
// check that whether [i] location character exists
// with between of [x to i] location or not
j = x;
while (j < i)
{
if (text[j] == text[i])
{
break;
}
j += 1;
}
if (j == i)
{
//When get a new substring
if ((j - x) > length)
{
// When get a newly longest unique substring
// Get length
length = j - x;
// Get starting location
front = x;
}
}
else
{
x = x + 1;
i = x;
}
i += 1;
}
print(" Given String : ", str ,"\n", terminator: "");
print(" Longest Unique Substring : ", terminator: "");
//Note that at least one unique character always exist in string
i = front;
while (i <= front + length)
{
print(text[i], terminator: "");
i += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyString = MyString();
obj.longest_unique_substring("ABCABD");
obj.longest_unique_substring("FBCSBSNL");
obj.longest_unique_substring("AABFBCAEBO");
obj.longest_unique_substring("AABAAAB");
obj.longest_unique_substring("BAACCAAB");
}
main();
Output
Given String : ABCABD
Longest Unique Substring : CABD
Given String : FBCSBSNL
Longest Unique Substring : FBCS
Given String : AABFBCAEBO
Longest Unique Substring : FBCAE
Given String : AABAAAB
Longest Unique Substring : AB
Given String : BAACCAAB
Longest Unique Substring : BA
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