Untitled

 avatar
unknown
golang
7 days ago
1.0 kB
7
Indexable
package main

import (
  "fmt"
)
/*

- should be case insensitive

aaaabbbbbabcdaaaaa
=> 4

abcabc
=>
a
b
...
c
ab
bc
ca
...
bc
abc
bca
cab
...
abca
bcab
...


""
=> 0

=> a

a : 1

=> generate all substrings
=> check if it has no repeating characters
=> return biggest value


*/

func hasUniqueChars(s string) bool{
  count := make(map[rune]int)
  for i := 0; i < len(s); i++ {
    _, ok := count[rune(s[i])]
    if ok {
      return false
    }
    count[rune(s[i])] = 1
  }
  return true
}

func longestSubstring(s string) int{
  //s = strings.toLower(s)
  list := make([]string, 0)
  for i := 0 ; i < len(s); i++ {
    for j := i; i+j < len(s); j++ {
      current := s[i:i+j+1]
      list = append(list, current)
    }
  }

  fmt.Println(list)

  max := -99999

  for _, s := range list {
    if hasUniqueChars(s) && len(s) > max {
      max = len(s)
    }
  }

  return max
}

func main() {
  fmt.Println(longestSubstring("abcabc"))
  fmt.Println(longestSubstring("abcabcd"))
  fmt.Println(longestSubstring("abcACB"))
}
Editor is loading...
Leave a Comment