返回介绍

solution / 1600-1699 / 1624.Largest Substring Between Two Equal Characters / README_EN

发布于 2024-06-17 01:03:16 字数 4346 浏览 0 评论 0 收藏 0

1624. Largest Substring Between Two Equal Characters

中文文档

Description

Given a string s, return _the length of the longest substring between two equal characters, excluding the two characters._ If there is no such substring return -1.

A substring is a contiguous sequence of characters within a string.

 

Example 1:

Input: s = "aa"
Output: 0
Explanation: The optimal substring here is an empty substring between the two 'a's.

Example 2:

Input: s = "abca"
Output: 2
Explanation: The optimal substring here is "bc".

Example 3:

Input: s = "cbzxy"
Output: -1
Explanation: There are no characters that appear twice in s.

 

Constraints:

  • 1 <= s.length <= 300
  • s contains only lowercase English letters.

Solutions

Solution 1

class Solution:
  def maxLengthBetweenEqualCharacters(self, s: str) -> int:
    d = {}
    ans = -1
    for i, c in enumerate(s):
      if c in d:
        ans = max(ans, i - d[c] - 1)
      else:
        d[c] = i
    return ans
class Solution {
  public int maxLengthBetweenEqualCharacters(String s) {
    int[] d = new int[26];
    Arrays.fill(d, -1);
    int ans = -1;
    for (int i = 0; i < s.length(); ++i) {
      int j = s.charAt(i) - 'a';
      if (d[j] == -1) {
        d[j] = i;
      } else {
        ans = Math.max(ans, i - d[j] - 1);
      }
    }
    return ans;
  }
}
class Solution {
public:
  int maxLengthBetweenEqualCharacters(string s) {
    vector<int> d(26, -1);
    int ans = -1;
    for (int i = 0; i < s.size(); ++i) {
      int j = s[i] - 'a';
      if (d[j] == -1) {
        d[j] = i;
      } else {
        ans = max(ans, i - d[j] - 1);
      }
    }
    return ans;
  }
};
func maxLengthBetweenEqualCharacters(s string) int {
  d := make([]int, 26)
  for i := range d {
    d[i] = -1
  }
  ans := -1
  for i, c := range s {
    c -= 'a'
    if d[c] == -1 {
      d[c] = i
    } else {
      ans = max(ans, i-d[c]-1)
    }
  }
  return ans
}
function maxLengthBetweenEqualCharacters(s: string): number {
  const n = s.length;
  const pos = new Array(26).fill(-1);
  let res = -1;
  for (let i = 0; i < n; i++) {
    const j = s[i].charCodeAt(0) - 'a'.charCodeAt(0);
    if (pos[j] === -1) {
      pos[j] = i;
    } else {
      res = Math.max(res, i - pos[j] - 1);
    }
  }
  return res;
}
impl Solution {
  pub fn max_length_between_equal_characters(s: String) -> i32 {
    let s = s.as_bytes();
    let n = s.len();
    let mut pos = [-1; 26];
    let mut res = -1;
    for i in 0..n {
      let j = (s[i] - b'a') as usize;
      let i = i as i32;
      if pos[j] == -1 {
        pos[j] = i;
      } else {
        res = res.max(i - pos[j] - 1);
      }
    }
    res
  }
}
#define max(a, b) (((a) > (b)) ? (a) : (b))

int maxLengthBetweenEqualCharacters(char* s) {
  int pos[26];
  memset(pos, -1, sizeof(pos));
  int n = strlen(s);
  int res = -1;
  for (int i = 0; i < n; i++) {
    char c = s[i];
    int j = c - 'a';
    if (pos[j] == -1) {
      pos[j] = i;
    } else {
      res = max(res, i - pos[j] - 1);
    }
  }
  return res;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文