Find longest substring with repeating characters
Here given code implementation process.
//C Program
//Find longest substring with repeating characters
#include <stdio.h>
//Function which is finding the longest repeating substring
void longest_repeating_char(char * text, int size)
{
if (size <= 1)
{
return;
}
//resultatant variables
int length = -1, counter = 0, location = 0;
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Loop iterating the string elements
for (i = 0; i < size; ++i)
{
//Reset the counter variable
counter = 0;
//Loop which is count the length of repeating substring
for (j = i + 1; j < size; j++)
{
if (text[j] == text[i])
{
//When same consecutive elements exist
counter++;
}
else
{
//When not same consecutive elements exist
break;
}
}
if (counter > 0 && counter > length)
{
//When get a newly longest repeating substring
location = i;
length = counter;
}
i += counter;
}
printf(" Given String : %s\n", text);
if (length == -1)
{
// If no result exists
printf(" Not found longest repeating substring\n");
return;
}
//When result are exists
printf(" Longest Repeating Substring : ");
for (int i = location; i <= location + length; ++i)
{
//Display resultant characters
printf("%c", text[i]);
}
printf("\n");
}
int main()
{
//Define the character elements
char str[] = "ABFFFFFAABD";
//Get the size
int size = sizeof(str) / sizeof(str[0]) - 1;
//Find result
longest_repeating_char(str, size);
//Other test cases
char str1[] = "ABCCCDCCCCCABBCC";
size = sizeof(str1) / sizeof(str1[0]) - 1;
longest_repeating_char(str1, size);
char str3[] = "AAAAAA";
size = sizeof(str3) / sizeof(str3[0]) - 1;
longest_repeating_char(str3, size);
char str4[] = "ABC";
size = sizeof(str4) / sizeof(str4[0]) - 1;
longest_repeating_char(str4, size);
return 0;
}
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
//Java Program
//Find longest substring with repeating characters
class MyString
{
//Function which is finding the longest repeating substring
public void longest_repeating_char(String text)
{
int size = text.length();
if (size <= 1)
{
return;
}
//resultatant variables
int length = -1, counter = 0, location = 0;
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Loop iterating the string elements
for (i = 0; i < size; ++i)
{
//Reset the counter variable
counter = 0;
//Loop which is count the length of repeating substring
for (j = i + 1; j < size; j++)
{
if (text.charAt(j) == text.charAt(i))
{
//When same consecutive elements exist
counter++;
}
else
{
break;
}
}
if (counter > 0 && counter > length)
{
//When get a newly longest repeating substring
location = i;
length = counter;
}
i += counter;
}
System.out.print(" Given String : " + text + "\n");
if (length == -1)
{
// If no result exists
System.out.print(" Not found longest repeating substring\n");
return;
}
//When result are exists
System.out.print(" Longest Repeating Substring : ");
for (i = location; i <= location + length; ++i)
{
System.out.print(text.charAt(i));
}
System.out.print("\n");
}
public static void main(String[] args)
{
MyString obj = new MyString();
obj.longest_repeating_char("ABFFFFFAABD");
obj.longest_repeating_char("ABCCCDCCCCCABBCC");
obj.longest_repeating_char("AAAAAA");
obj.longest_repeating_char("ABC");
}
}
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
//Include header file
#include <iostream>
#include<string.h>
using namespace std;
//C++ Program
//Find longest substring with repeating characters
class MyString
{
public:
//Function which is finding the longest repeating substring
void longest_repeating_char(string text)
{
int size = text.size();
if (size <= 1)
{
return;
}
//resultatant variables
int length = -1, counter = 0, location = 0;
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Loop iterating the string elements
for (i = 0; i < size; ++i)
{
//Reset the counter variable
counter = 0;
//Loop which is count the length of repeating substring
for (j = i + 1; j < size; j++)
{
if (text[j] == text[i])
{
//When same consecutive elements exist
counter++;
}
else
{
break;
}
}
if (counter > 0 && counter > length)
{
//When get a newly longest repeating substring
location = i;
length = counter;
}
i += counter;
}
cout << " Given String : " << text << "\n";
if (length == -1)
{
// If no result exists
cout << " Not found longest repeating substring\n";
return;
}
//When result are exists
cout << " Longest Repeating Substring : ";
for (i = location; i <= location + length; ++i)
{
cout << text[i];
}
cout << "\n";
}
};
int main()
{
MyString obj = MyString();
obj.longest_repeating_char("ABFFFFFAABD");
obj.longest_repeating_char("ABCCCDCCCCCABBCC");
obj.longest_repeating_char("AAAAAA");
obj.longest_repeating_char("ABC");
return 0;
}
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
//Include namespace system
using System;
//C# Program
//Find longest substring with repeating characters
class MyString
{
//Function which is finding the longest repeating substring
public void longest_repeating_char(String text)
{
int size = text.Length;
if (size <= 1)
{
return;
}
//resultatant variables
int length = -1, counter = 0, location = 0;
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Loop iterating the string elements
for (i = 0; i < size; ++i)
{
//Reset the counter variable
counter = 0;
//Loop which is count the length of repeating substring
for (j = i + 1; j < size; j++)
{
if (text[j] == text[i])
{
//When same consecutive elements exist
counter++;
}
else
{
break;
}
}
if (counter > 0 && counter > length)
{
//When get a newly longest repeating substring
location = i;
length = counter;
}
i += counter;
}
Console.Write(" Given String : " + text + "\n");
if (length == -1)
{
// If no result exists
Console.Write(" Not found longest repeating substring\n");
return;
}
//When result are exists
Console.Write(" Longest Repeating Substring : ");
for (i = location; i <= location + length; ++i)
{
Console.Write(text[i]);
}
Console.Write("\n");
}
public static void Main(String[] args)
{
MyString obj = new MyString();
obj.longest_repeating_char("ABFFFFFAABD");
obj.longest_repeating_char("ABCCCDCCCCCABBCC");
obj.longest_repeating_char("AAAAAA");
obj.longest_repeating_char("ABC");
}
}
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
<?php
//Php Program
//Find longest substring with repeating characters
class MyString
{
//Function which is finding the longest repeating substring
public function longest_repeating_char($text)
{
$size = strlen($text);
if ($size <= 1)
{
return;
}
//resultatant variables
$length = -1;
$counter = 0;
$location = 0;
//Loop controlling variable [i,j]
$i = 0;
$j = 0;
//Loop iterating the string elements
for ($i = 0; $i < $size; ++$i)
{
//Reset the counter variable
$counter = 0;
//Loop which is count the length of repeating substring
for ($j = $i + 1; $j < $size; $j++)
{
if ($text[$j] == $text[$i])
{
//When same consecutive elements exist
$counter++;
}
else
{
break;
}
}
if ($counter > 0 && $counter > $length)
{
//When get a newly longest repeating substring
$location = $i;
$length = $counter;
}
$i += $counter;
}
echo " Given String : ". $text ."\n";
if ($length == -1)
{
echo " Not found longest repeating substring\n";
return;
}
echo " Longest Repeating Substring : ";
for ($i = $location; $i <= $location + $length; ++$i)
{
echo $text[$i];
}
echo "\n";
}
}
function main()
{
$obj = new MyString();
$obj->longest_repeating_char("ABFFFFFAABD");
$obj->longest_repeating_char("ABCCCDCCCCCABBCC");
$obj->longest_repeating_char("AAAAAA");
$obj->longest_repeating_char("ABC");
}
main();
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
//Node Js Program
//Find longest substring with repeating characters
class MyString
{
//Function which is finding the longest repeating substring
longest_repeating_char(text)
{
var size = text.length;
if (size <= 1)
{
return;
}
//resultatant variables
var length = -1;
var counter = 0;
var location = 0;
//Loop controlling variable [i,j]
var i = 0;
var j = 0;
//Loop iterating the string elements
for (i = 0; i < size; ++i)
{
//Reset the counter variable
counter = 0;
//Loop which is count the length of repeating substring
for (j = i + 1; j < size; j++)
{
if (text[j] == text[i])
{
//When same consecutive elements exist
counter++;
}
else
{
break;
}
}
if (counter > 0 && counter > length)
{
//When get a newly longest repeating substring
location = i;
length = counter;
}
i += counter;
}
process.stdout.write(" Given String : " + text + "\n");
if (length == -1)
{
process.stdout.write(" Not found longest repeating substring\n");
return;
}
process.stdout.write(" Longest Repeating Substring : ");
for (i = location; i <= location + length; ++i)
{
process.stdout.write(text[i]);
}
process.stdout.write("\n");
}
}
function main()
{
var obj = new MyString();
obj.longest_repeating_char("ABFFFFFAABD");
obj.longest_repeating_char("ABCCCDCCCCCABBCC");
obj.longest_repeating_char("AAAAAA");
obj.longest_repeating_char("ABC");
}
main();
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
# Python 3 Program
# Find longest substring with repeating characters
class MyString :
# Function which is finding the longest repeating substring
def longest_repeating_char(self, text) :
size = len(text)
if (size <= 1) :
return
# resultatant variables
length = -1
counter = 0
location = 0
# Loop controlling variable [i,j]
i = 0
j = 0
# Loop iterating the string elements
while (i < size) :
# Reset the counter variable
counter = 0
j = i + 1
# Loop which is count the length of repeating substring
while (j < size) :
if (text[j] == text[i]) :
# When same consecutive elements exist
counter += 1
else :
break
j += 1
if (counter > 0 and counter > length) :
# When get a newly longest repeating substring
location = i
length = counter
i += counter
i += 1
print(" Given String : ", text ,"\n", end = "")
if (length == -1) :
print(" Not found longest repeating substring\n", end = "")
return
print(" Longest Repeating Substring : ", end = "")
i = location
while (i <= location + length) :
print(text[i], end = "")
i += 1
print("\n", end = "")
def main() :
obj = MyString()
obj.longest_repeating_char("ABFFFFFAABD")
obj.longest_repeating_char("ABCCCDCCCCCABBCC")
obj.longest_repeating_char("AAAAAA")
obj.longest_repeating_char("ABC")
if __name__ == "__main__": main()
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
# Ruby Program
# Find longest substring with repeating characters
class MyString
# Function which is finding the longest repeating substring
def longest_repeating_char(text)
size = text.length()
if (size <= 1)
return
end
# resultatant variables
length = -1
counter = 0
location = 0
# Loop controlling variable [i,j]
i = 0
j = 0
# Loop iterating the string elements
while (i < size)
# Reset the counter variable
counter = 0
j = i + 1
# Loop which is count the length of repeating substring
while (j < size)
if (text[j] == text[i])
# When same consecutive elements exist
counter += 1
else
break
end
j += 1
end
if (counter > 0 && counter > length)
# When get a newly longest repeating substring
location = i
length = counter
end
i += counter
i += 1
end
print(" Given String : ", text ,"\n")
if (length == -1)
# If no result exists
print(" Not found longest repeating substring\n")
return
end
# When result are exists
print(" Longest Repeating Substring : ")
i = location
while (i <= location + length)
print(text[i])
i += 1
end
print("\n")
end
end
def main()
obj = MyString.new()
obj.longest_repeating_char("ABFFFFFAABD")
obj.longest_repeating_char("ABCCCDCCCCCABBCC")
obj.longest_repeating_char("AAAAAA")
obj.longest_repeating_char("ABC")
end
main()
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
import scala.util.control.Breaks._
//Scala Program
//Find longest substring with repeating characters
class MyString
{
//Function which is finding the longest repeating substring
def longest_repeating_char(text: String): Unit = {
var size: Int = text.length();
if (size <= 1)
{
return;
}
//resultatant variables
var length: Int = -1;
var counter: Int = 0;
var location: Int = 0;
//Loop controlling variable [i,j]
var i: Int = 0;
var j: Int = 0;
//Loop iterating the string elements
while (i < size)
{
//Reset the counter variable
counter = 0;
j = i + 1;
breakable {
//Loop which is count the length of repeating substring
while (j < size)
{
if (text(j) == text(i))
{
//When same consecutive elements exist
counter += 1;
}
else
{
break;
}
j += 1;
}
}
if (counter > 0 && counter > length)
{
//When get a newly longest repeating substring
location = i;
length = counter;
}
i += counter;
i += 1;
}
print(" Given String : " + text + "\n");
if (length == -1)
{
// If no result exists
print(" Not found longest repeating substring\n");
return;
}
//When result are exists
print(" Longest Repeating Substring : ");
i = location;
while (i <= location + length)
{
print(text(i));
i += 1;
}
print("\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyString = new MyString();
obj.longest_repeating_char("ABFFFFFAABD");
obj.longest_repeating_char("ABCCCDCCCCCABBCC");
obj.longest_repeating_char("AAAAAA");
obj.longest_repeating_char("ABC");
}
}
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
//Swift Program
//Find longest substring with repeating characters
class MyString
{
//Function which is finding the longest repeating substring
func longest_repeating_char(_ text: String)
{
let size: Int = text.count;
let arr = Array(text);
if (size <= 1)
{
return;
}
//resultatant variables
var length: Int = -1;
var counter: Int = 0;
var location: Int = 0;
//Loop controlling variable [i,j]
var i: Int = 0;
var j: Int = 0;
//Loop iterating the string elements
while (i < size)
{
//Reset the counter variable
counter = 0;
j = i + 1;
//Loop which is count the length of repeating substring
while (j < size)
{
if (arr[j] == arr[i])
{
//When same consecutive elements exist
counter += 1;
}
else
{
break;
}
j += 1;
}
if (counter > 0 && counter > length)
{
//When get a newly longest repeating substring
location = i;
length = counter;
}
i += counter;
i += 1;
}
print(" Given String : ", text ,"\n", terminator: "");
if (length == -1)
{
print(" Not found longest repeating substring\n", terminator: "");
return;
}
print(" Longest Repeating Substring : ", terminator: "");
i = location;
while (i <= location + length)
{
print(arr[i], terminator: "");
i += 1;
}
print("\n", terminator: "");
}
}
func main()
{
let obj: MyString = MyString();
obj.longest_repeating_char("ABFFFFFAABD");
obj.longest_repeating_char("ABCCCDCCCCCABBCC");
obj.longest_repeating_char("AAAAAA");
obj.longest_repeating_char("ABC");
}
main();
Output
Given String : ABFFFFFAABD
Longest Repeating Substring : FFFFF
Given String : ABCCCDCCCCCABBCC
Longest Repeating Substring : CCCCC
Given String : AAAAAA
Longest Repeating Substring : AAAAAA
Given String : ABC
Not found longest repeating substring
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