📜  Golang中求不重复字符的最长子串长度的程序

📅  最后修改于: 2021-10-25 02:16:41             🧑  作者: Mango

给定一个字符串,在 Golang 中打印不重复字符的最长子字符串。例如,“ABDEFGABEF”没有重复字符的最长子串是“BDEFGA”。

例子:

Input : GEEKSFORGEEKS
Output : 
Substring: EKSFORG
Length: 7

Input : ABDEFGABEF
Output : 
Substring: BDEFGA
Length: 6

这个想法是遍历字符串,对于每个已经访问过的字符,将其最后一次出现在整数数组pos中(我们将根据字符串字符的 ASCII 值更新数组中的索引)。变量st存储当前子串的起始点, maxlen存储最大长度子串的长度,start存储最大长度子串的起始索引。

在遍历字符串,通过检查数组pos来检查当前字符是否已经存在于字符串。如果不存在,则将当前字符的索引存储在数组中,并将 value 作为当前索引。如果它已经存在于数组中,这意味着当前字符可以在当前子字符串中重复。为此,请检查上一次出现的字符是在当前子字符串的起点 st 之前还是之后。如果它在st之前,则只更新数组中的值。如果在 st 之后,则求当前子串currlen的长度为i-st ,其中i 是当前索引。

currlenmaxlen进行比较。如果maxlen小于 currlen,则将 maxlen 更新为currlen并从 st 开始。完全遍历字符串,需要的最长不重复字符的子串是从s[start] 到 s[start+maxlen-1]

// 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)
  
}

输出:

Longest substring is:  EKSFORG
Length of the string:  7

注意:除了使用数组,我们还可以使用字符串int映射