Skip to content

Commit 0caef8c

Browse files
committed
feat: add solutions to lc problem: No.1297
1 parent 52dfdb8 commit 0caef8c

File tree

10 files changed

+1179
-4
lines changed

10 files changed

+1179
-4
lines changed

solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README.md

Lines changed: 410 additions & 1 deletion
Large diffs are not rendered by default.

solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/README_EN.md

Lines changed: 411 additions & 1 deletion
Large diffs are not rendered by default.

solution/1200-1299/1297.Maximum Number of Occurrences of a Substring/Solution.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
99
ss.add(t.charAt(j));
1010
}
1111
if (ss.size() <= maxLetters) {
12-
cnt.put(t, cnt.getOrDefault(t, 0) + 1);
12+
cnt.merge(t, 1, Integer::sum);
1313
ans = Math.max(ans, cnt.get(t));
1414
}
1515
}
1616
return ans;
1717
}
18-
}
18+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use std::collections::{HashMap, HashSet};
2+
3+
impl Solution {
4+
pub fn max_freq(s: String, max_letters: i32, min_size: i32, _max_size: i32) -> i32 {
5+
let n = s.len();
6+
let m = min_size as usize;
7+
let max_letters = max_letters as usize;
8+
let bytes = s.as_bytes();
9+
let mut cnt: HashMap<&[u8], i32> = HashMap::new();
10+
let mut ans = 0;
11+
12+
for i in 0..=n - m {
13+
let t = &bytes[i..i + m];
14+
15+
let mut set = HashSet::new();
16+
for &c in t {
17+
set.insert(c);
18+
if set.len() > max_letters {
19+
break;
20+
}
21+
}
22+
if set.len() <= max_letters {
23+
let v = cnt.entry(t).or_insert(0);
24+
*v += 1;
25+
ans = ans.max(*v);
26+
}
27+
}
28+
29+
ans
30+
}
31+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
class Hashing {
2+
public:
3+
vector<long long> p, h;
4+
long long mod;
5+
6+
Hashing(const string& word)
7+
: Hashing(word, 13331, 998244353) {}
8+
9+
Hashing(const string& word, long long base, long long mod)
10+
: mod(mod) {
11+
int n = word.size();
12+
p.assign(n + 1, 1);
13+
h.assign(n + 1, 0);
14+
for (int i = 1; i <= n; i++) {
15+
p[i] = p[i - 1] * base % mod;
16+
h[i] = (h[i - 1] * base + word[i - 1]) % mod;
17+
}
18+
}
19+
20+
long long query(int l, int r) {
21+
return (h[r] - h[l - 1] * p[r - l + 1] % mod + mod) % mod;
22+
}
23+
};
24+
25+
class Solution {
26+
public:
27+
int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
28+
int n = s.size();
29+
Hashing hashing(s);
30+
vector<int> freq(256, 0);
31+
int k = 0;
32+
int ans = 0;
33+
unordered_map<long long, int> cnt;
34+
35+
for (int i = 1; i <= n; i++) {
36+
if (++freq[s[i - 1]] == 1) {
37+
k++;
38+
}
39+
40+
if (i >= minSize) {
41+
if (k <= maxLetters) {
42+
long long x = hashing.query(i - minSize + 1, i);
43+
ans = max(ans, ++cnt[x]);
44+
}
45+
int j = i - minSize;
46+
if (--freq[s[j]] == 0) {
47+
k--;
48+
}
49+
}
50+
}
51+
52+
return ans;
53+
}
54+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
func maxFreq(s string, maxLetters int, minSize int, maxSize int) int {
2+
n := len(s)
3+
hashing := NewHashing(s)
4+
freq := make([]int, 256)
5+
k := 0
6+
ans := 0
7+
cnt := make(map[uint64]int)
8+
9+
for i := 1; i <= n; i++ {
10+
c := s[i-1]
11+
freq[c]++
12+
if freq[c] == 1 {
13+
k++
14+
}
15+
16+
if i >= minSize {
17+
if k <= maxLetters {
18+
x := hashing.Query(i-minSize+1, i)
19+
cnt[x]++
20+
if cnt[x] > ans {
21+
ans = cnt[x]
22+
}
23+
}
24+
j := i - minSize
25+
c2 := s[j]
26+
freq[c2]--
27+
if freq[c2] == 0 {
28+
k--
29+
}
30+
}
31+
}
32+
33+
return ans
34+
}
35+
36+
type Hashing struct {
37+
p, h []uint64
38+
mod uint64
39+
base uint64
40+
}
41+
42+
func NewHashing(word string) *Hashing {
43+
return NewHashingWithBase(word, 13331, 998244353)
44+
}
45+
46+
func NewHashingWithBase(word string, base uint64, mod uint64) *Hashing {
47+
n := len(word)
48+
p := make([]uint64, n+1)
49+
h := make([]uint64, n+1)
50+
p[0] = 1
51+
for i := 1; i <= n; i++ {
52+
p[i] = p[i-1] * base % mod
53+
h[i] = (h[i-1]*base + uint64(word[i-1])) % mod
54+
}
55+
return &Hashing{p, h, mod, base}
56+
}
57+
58+
func (hs *Hashing) Query(l, r int) uint64 {
59+
return (hs.h[r] + hs.mod - hs.h[l-1]*hs.p[r-l+1]%hs.mod) % hs.mod
60+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Hashing {
2+
private final long[] p;
3+
private final long[] h;
4+
private final long mod;
5+
6+
public Hashing(String word) {
7+
this(word, 13331, 998244353);
8+
}
9+
10+
public Hashing(String word, long base, int mod) {
11+
int n = word.length();
12+
p = new long[n + 1];
13+
h = new long[n + 1];
14+
p[0] = 1;
15+
this.mod = mod;
16+
for (int i = 1; i <= n; i++) {
17+
p[i] = p[i - 1] * base % mod;
18+
h[i] = (h[i - 1] * base + word.charAt(i - 1)) % mod;
19+
}
20+
}
21+
22+
public long query(int l, int r) {
23+
return (h[r] - h[l - 1] * p[r - l + 1] % mod + mod) % mod;
24+
}
25+
}
26+
27+
class Solution {
28+
public int maxFreq(String s, int maxLetters, int minSize, int maxSize) {
29+
int n = s.length();
30+
Hashing hashing = new Hashing(s);
31+
int[] freq = new int[256];
32+
int k = 0;
33+
int ans = 0;
34+
Map<Long, Integer> cnt = new HashMap<>();
35+
36+
for (int i = 1; i <= n; i++) {
37+
if (++freq[s.charAt(i - 1)] == 1) {
38+
k++;
39+
}
40+
41+
if (i >= minSize) {
42+
if (k <= maxLetters) {
43+
long x = hashing.query(i - minSize + 1, i);
44+
ans = Math.max(ans, cnt.merge(x, 1, Integer::sum));
45+
}
46+
int j = i - minSize;
47+
if (--freq[s.charAt(j)] == 0) {
48+
k--;
49+
}
50+
}
51+
}
52+
53+
return ans;
54+
}
55+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Hashing:
2+
__slots__ = ["mod", "h", "p"]
3+
4+
def __init__(
5+
self, s: Union[str, List[str]], base: int = 13331, mod: int = 998244353
6+
):
7+
self.mod = mod
8+
self.h = [0] * (len(s) + 1)
9+
self.p = [1] * (len(s) + 1)
10+
for i in range(1, len(s) + 1):
11+
self.h[i] = (self.h[i - 1] * base + ord(s[i - 1])) % mod
12+
self.p[i] = (self.p[i - 1] * base) % mod
13+
14+
def query(self, l: int, r: int) -> int:
15+
return (self.h[r] - self.h[l - 1] * self.p[r - l + 1]) % self.mod
16+
17+
18+
class Solution:
19+
def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int:
20+
freq = Counter()
21+
hashing = Hashing(s)
22+
cnt = Counter()
23+
ans = k = 0
24+
for i, c in enumerate(s, 1):
25+
freq[c] += 1
26+
if freq[c] == 1:
27+
k += 1
28+
if i >= minSize:
29+
if k <= maxLetters:
30+
x = hashing.query(i - minSize + 1, i)
31+
cnt[x] += 1
32+
ans = max(ans, cnt[x])
33+
j = i - minSize
34+
freq[s[j]] -= 1
35+
if freq[s[j]] == 0:
36+
k -= 1
37+
return ans
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
impl Solution {
2+
pub fn max_freq(s: String, max_letters: i32, min_size: i32, _max_size: i32) -> i32 {
3+
let n = s.len();
4+
let bytes = s.as_bytes();
5+
let hashing = Hashing::new(bytes.to_vec());
6+
let mut freq = [0i32; 256];
7+
let mut k = 0;
8+
let mut ans = 0;
9+
let mut cnt: std::collections::HashMap<u64, i32> = std::collections::HashMap::new();
10+
11+
for i in 1..=n {
12+
let c = bytes[i - 1] as usize;
13+
freq[c] += 1;
14+
if freq[c] == 1 {
15+
k += 1;
16+
}
17+
18+
if i as i32 >= min_size {
19+
if k <= max_letters {
20+
let x = hashing.query(i - min_size as usize, i - 1);
21+
let v = cnt.entry(x).and_modify(|v| *v += 1).or_insert(1);
22+
ans = ans.max(*v);
23+
}
24+
let j = i - min_size as usize;
25+
let c2 = bytes[j] as usize;
26+
freq[c2] -= 1;
27+
if freq[c2] == 0 {
28+
k -= 1;
29+
}
30+
}
31+
}
32+
33+
ans
34+
}
35+
}
36+
37+
struct Hashing {
38+
p: Vec<u64>,
39+
h: Vec<u64>,
40+
base: u64,
41+
modv: u64,
42+
}
43+
44+
impl Hashing {
45+
fn new(s: Vec<u8>) -> Self {
46+
Self::with_params(s, 13331, 998244353)
47+
}
48+
49+
fn with_params(s: Vec<u8>, base: u64, modv: u64) -> Self {
50+
let n = s.len();
51+
let mut p = vec![0u64; n + 1];
52+
let mut h = vec![0u64; n + 1];
53+
p[0] = 1;
54+
for i in 1..=n {
55+
p[i] = p[i - 1].wrapping_mul(base) % modv;
56+
h[i] = (h[i - 1].wrapping_mul(base) + s[i - 1] as u64) % modv;
57+
}
58+
Self { p, h, base, modv }
59+
}
60+
61+
fn query(&self, l: usize, r: usize) -> u64 {
62+
let mut res =
63+
self.h[r + 1] + self.modv - (self.h[l] * self.p[r - l + 1] % self.modv);
64+
res %= self.modv;
65+
res
66+
}
67+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
class Hashing {
2+
private p: bigint[];
3+
private h: bigint[];
4+
private mod: bigint;
5+
6+
constructor(s: string, base: bigint = 13331n, mod: bigint = 998244353n) {
7+
const n = s.length;
8+
this.mod = mod;
9+
this.p = new Array<bigint>(n + 1).fill(1n);
10+
this.h = new Array<bigint>(n + 1).fill(0n);
11+
for (let i = 1; i <= n; i++) {
12+
this.p[i] = (this.p[i - 1] * base) % mod;
13+
this.h[i] = (this.h[i - 1] * base + BigInt(s.charCodeAt(i - 1))) % mod;
14+
}
15+
}
16+
17+
query(l: number, r: number): bigint {
18+
return (this.h[r] - ((this.h[l - 1] * this.p[r - l + 1]) % this.mod) + this.mod) % this.mod;
19+
}
20+
}
21+
22+
function maxFreq(s: string, maxLetters: number, minSize: number, maxSize: number): number {
23+
const n = s.length;
24+
const hashing = new Hashing(s);
25+
const freq = new Array<number>(256).fill(0);
26+
let k = 0;
27+
let ans = 0;
28+
const cnt = new Map<bigint, number>();
29+
30+
for (let i = 1; i <= n; i++) {
31+
const c = s.charCodeAt(i - 1);
32+
if (++freq[c] === 1) {
33+
k++;
34+
}
35+
36+
if (i >= minSize) {
37+
if (k <= maxLetters) {
38+
const x = hashing.query(i - minSize + 1, i);
39+
const v = (cnt.get(x) || 0) + 1;
40+
cnt.set(x, v);
41+
ans = Math.max(ans, v);
42+
}
43+
const j = i - minSize;
44+
const c2 = s.charCodeAt(j);
45+
if (--freq[c2] === 0) {
46+
k--;
47+
}
48+
}
49+
}
50+
51+
return ans;
52+
}

0 commit comments

Comments
 (0)