Leetcode 1839 (简单思考的乐趣)

1839. 所有元音按顺序排布的最长子字符串
当一个字符串满足如下条件时,我们称它是 美丽的 :
所有 5 个英文元音字母('a' ,'e' ,'i' ,'o' ,'u')都必须 至少 出现一次。
这些元音字母的顺序都必须按照 字典序 升序排布(也就是说所有的 'a' 都在 'e' 前面,所有的 'e' 都在 'i' 前面,以此类推)
比方说,字符串 "aeiou" 和 "aaaaaaeiiiioou" 都是 美丽的 ,但是 "uaeio" ,"aeoiu" 和 "aaaeeeooo" 不是美丽的 。
给你一个只包含英文元音字母的字符串 word ,请你返回 word 中 最长美丽子字符串的长度 。如果不存在这样的子字符串,请返回 0 。
子字符串 是字符串中一个连续的字符序列。

示例 1:

输入:word = "aeiaaioaaaaeiiiiouuuooaauuaeiu"
输出:13
解释:最长子字符串是 "aaaaeiiiiouuu" ,长度为 13 。

示例 2:

输入:word = "aeeeiiiioooauuuaeiou"
输出:5
解释:最长子字符串是 "aeiou" ,长度为 5 。

示例 3:

输入:word = "a"
输出:0
解释:没有美丽子字符串,所以返回 0 。

提示:

1 <= word.length <= 5 * 10**5
word 只包含字符 'a','e','i','o' 和 'u' 。

题解:

# @param {String} word
# @return {Integer}
def longest_beautiful_substring(word)
  len = word.length
  i = 0
  l = 0
  h = {"a" => 1,"e" => 2,"i" => 3,"o" => 4,"u" => 5}
  t = [h[word[0]]]
  h1 = {}
  while i < len
    if i+1 < len
      if i == 0 && t[-1] == 1 && h[word[i+1]] - t[-1] <= 1 && h[word[i+1]] >= t[-1]
        t << h[word[i+1]]
        h1[t[0]] = 1
        h1[t[1]] = 1
        i += 1
      elsif i == 0 && t[-1] > 1
        t.clear
        if h[word[i+1]] == 1
          t << h[word[i+1]]
          h1[h[word[i+1]]] = 1
          i += 1
        else
          i += 1
        end
      elsif i == 0 && t[-1] == 1 && (h[word[i+1]] - t[-1] > 1 || h[word[i+1]] < t[-1])
        t.clear
        if h[word[i+1]] == 1
          t << h[word[i+1]]
          h1[h[word[i+1]]] = 1
          i += 1
        else
          i += 1
        end
      elsif i > 0 && t.length > 0 && h[word[i+1]] - t[-1] <= 1 && h[word[i+1]] >= t[-1]
        t << h[word[i+1]]
        h1[h[word[i+1]]] = 1
        i += 1
      elsif i > 0 && t.length > 0 && (h[word[i+1]] - t[-1] > 1 || h[word[i+1]] < t[-1])
        if h1.length == 5
          l = [l,t.length].max
          t.clear
          h1.clear
          if h[word[i+1]] == 1
            t << h[word[i+1]]
            h1[h[word[i+1]]] = 1
            i += 1
          else
            i += 1
          end
        else
          t.clear
          h1.clear
          if h[word[i+1]] == 1
            t << h[word[i+1]]
            h1[h[word[i+1]]] = 1
            i += 1
          else
            i += 1
          end
        end
      elsif i > 0 && t.length == 0 && h[word[i+1]] == 1
        t << h[word[i+1]]
        h1[t[0]] = 1
        i += 1
      elsif i > 0 && t.length == 0 && h[word[i+1]] > 1
        i += 1
      end
    else
      if h1.length == 5
        l = [l,t.length].max
        break
      else
        break
      end
    end
  end
  l
end

空间优化题解

# @param {String} word
# @return {Integer}
def longest_beautiful_substring(word)
  len = word.length
  i = 0
  l = 0
  h = {"a" => 1,"e" => 2,"i" => 3,"o" => 4,"u" => 5}
  x = h[word[0]]
  t = 0
  h1 = {}
  while i < len
    if i+1 < len
      if i == 0 && x == 1 && h[word[i+1]] - x <= 1 && h[word[i+1]] >= x
        t = 1
        h1[x] = 1
        h1[h[word[i+1]]] = 1
        x = h[word[i+1]]
        i += 1
        t += 1
      elsif i == 0 && x > 1
        t = 0
        x = 0
        if h[word[i+1]] == 1
          x = h[word[i+1]]
          h1[x] = 1
          i += 1
          t += 1
        else
          i += 1
        end
      elsif i == 0 && x == 1 && (h[word[i+1]] - x > 1 || h[word[i+1]] < x)
        t = 0
        x = 0
        if h[word[i+1]] == 1
          x = h[word[i+1]]
          h1[x] = 1
          t += 1
          i += 1
        else
          i += 1
        end
      elsif i > 0 && t > 0 && h[word[i+1]] - x <= 1 && h[word[i+1]] >= x
        x = h[word[i+1]]
        h1[x] = 1
        i += 1
        t += 1
      elsif i > 0 && t > 0 && (h[word[i+1]] - x > 1 || h[word[i+1]] < x)
        if h1.length == 5
          l = [l,t].max
          t = 0
          h1.clear
          if h[word[i+1]] == 1
            x = h[word[i+1]]
            h1[x] = 1
            t += 1
            i += 1
          else
            i += 1
          end
        else
          t = 0
          h1.clear
          if h[word[i+1]] == 1
            x = h[word[i+1]]
            h1[x] = 1
            i += 1
            t += 1
          else
            i += 1
          end
        end
      elsif i > 0 && t == 0 && h[word[i+1]] == 1
        x = h[word[i+1]]
        h1[x] = 1
        t += 1
        i += 1
      elsif i > 0 && t == 0 && h[word[i+1]] > 1
        i += 1
      end
    else
      if h1.length == 5
        l = [l,t].max
        break
      else
        break
      end
    end
  end
  l
end
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容