Program to find the Length of the longest substring without repeating characters in Golang
Last Updated :
10 May, 2020
Given a string, print the longest substring without repeating characters in Golang. For example, the longest substrings without repeating characters for “ABDEFGABEF” are “BDEFGA”.
Examples:
Input : GEEKSFORGEEKS
Output :
Substring: EKSFORG
Length: 7
Input : ABDEFGABEF
Output :
Substring: BDEFGA
Length: 6
The idea is to traverse the string and for each already visited character store its
last occurrence in an array
pos of integers(we will update the indexes in the array based on the ASCII values of the characters of the string). The variable
st stores starting point of current substring,
maxlen stores length of maximum length substring, and start stores starting index of maximum length substring.
While traversing the string, check whether the current character is already present in the string by checking out the array
pos. If it is not present, then store the current character's index in the array with value as the current index. If it is already present in the array, this means the current character could repeat in the current substring. For this, check if the previous occurrence of character is before or after the starting point st of the current substring. If it is before
st, then only update the value in array. If it is after st, then find the length of current substring
currlen as
i-st, where
i is current index.
Compare
currlen with
maxlen. If
maxlen is less than currlen, then update maxlen as
currlen and start as st. After complete traversal of string, the required longest substring without repeating characters is from
s[start] to s[start+maxlen-1].
C
// Golang program to find the length of the
// longest substring without repeating
// characters
package main
import "fmt"
func longest_substring(s string, n int) string {
var i int
// starting point of
// current substring.
st := 0
// length of current substring.
currlen := 0
// maximum length substring
// without repeating characters
maxlen := 0
// starting index of
// maximum length substring.
start := 0
// this array works as the hash table
// -1 indicates that element is not
// present before else any value
// indicates its previous index
pos := make([]int, 125)
for i = 0; i < 125; i++ {
pos[i] = -1
}
// storing the index
// of first character
pos[s[0]] = 0
for i = 1; i < n; i++ {
// If this character is not present in array,
// then this is first occurrence of this
// character, store this in array.
if pos[s[i]] == -1 {
pos[s[i]] = i
} else {
// If this character is present in hash then
// this character has previous occurrence,
// check if that occurrence is before or after
// starting point of current substring.
if pos[s[i]] >= st {
// find length of current substring and
// update maxlen and start accordingly.
currlen = i - st
if maxlen < currlen {
maxlen = currlen
start = st
}
// Next substring will start after the last
// occurrence of current character to avoid
// its repetition.
st = pos[s[i]] + 1
}
// Update last occurrence of
// current character.
pos[s[i]] = i
}
}
// Compare length of last substring
// with maxlen and update maxlen
// and start accordingly.
if maxlen < i-st {
maxlen = i - st
start = st
}
// the required string
ans := ""
// extracting the string from
// [start] to [start+maxlen]
for i = start; i < start+maxlen; i++ {
ans += string(s[i])
}
return ans
}
func main() {
var s string = "GEEKSFORGEEKS"
var n int = len(s)
// calling the function to
// get the required answer.
var newString = longest_substring(s, n)
// finding the length of the string
var length int = len(newString)
fmt.Println("Longest substring is: ", newString)
fmt.Println("Length of the string: ", length)
}
Output:
Longest substring is: EKSFORG
Length of the string: 7
Note: Instead of using the array, we can also use a
map of
string to
int.
Similar Reads
Count K-Length Substrings With No Repeated Characters Given a string S and an integer k, the task is to return the number of substrings in S of length k with no repeated characters.Example:Input: S = "geeksforgeeks", k = 5Output: 4Explanation: There are 4 substrings, they are: 'eksfo', 'ksfor', 'sforg', 'forge'.Input: S = "home", k = 5Output: 0Explanat
6 min read
How to find the Length of Channel, Pointer, Slice, String and Map in Golang? In Golang, len function is used to find the length of a channel, pointer, slice, string, and map. Channel: In Go language, a channel is a medium through which a goroutine communicates with another goroutine and this communication is lock-free. C // Go program to illustrate // how to find the length
2 min read
How to find the length of the pointer in Golang? Pointers in Go programming language or Golang is a variable which is used to store the memory address of another variable. Pointers in Golang are also termed as the special variables. The variables are used to store some data at a particular memory address in the system. The memory address is always
2 min read
Find the Longest Non-Prefix-Suffix Substring in the Given String Given a string s of length n. The task is to determine the longest substring t such that t is neither the prefix nor the suffix of string s, and that substring must appear as both prefix and suffix of the string s. If no such string exists, print -1. Example: Input: s = "fixprefixsuffix"Output: fix
7 min read
Counting number of repeating words in a Golang String Given a string, the task is to count the number of words being repeated in that particular string in Golang. Example: Input: s = "She is mother of my mother." Output: She = 1 is = 1 mother = 2 of = 1 my = 1 To count the number of repeating words, first, the string is taken as input and then strings.
2 min read