Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

最长重复子串 #449

Open
Pcjmy opened this issue Feb 3, 2023 · 2 comments
Open

最长重复子串 #449

Pcjmy opened this issue Feb 3, 2023 · 2 comments

Comments

@Pcjmy
Copy link
Contributor

Pcjmy commented Feb 3, 2023

No description provided.

@rhwoodpecker
Copy link

rhwoodpecker commented Mar 7, 2023

for循环大法

function getLongestRepeatString(s) {
    if (!s) return s;
    let ans = '';
    const map = new Set();
    for (let i = 0; i < s.length; i++) {
        for (let j = i + 1; j <= s.length; j++) {
            const chs = s.substring(i, j);
            if (map.has(chs) && chs.length > ans.length) {
                ans = chs;
            }

            map.add(chs);
        }
    }
    return ans;
}

console.log('abcbcabc =>', getLongestRepeatString('abcbcabc'));
//  abcbcabc => abc
console.log('abcabcacbd =>', getLongestRepeatString('abcabcacbd'));
//   abcabcacbd => abca

@topulikeweb
Copy link

function getMaxLengthChdStr (str) {
  let right = 0
  let left = 0
  const set = new Set()
  while (right < str.length) {
    if (set.has(str[right])) {
      set.delete(str[right])
      left++
    } else {
      set.add(str[right])
      right++
    }
  }
  return right - left
}

console.log(getMaxLengthChdStr('abcdaccc'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants