From e49fe51d29043b42ff8c64053866c454d497f34b Mon Sep 17 00:00:00 2001 From: aucker Date: Mon, 20 May 2024 11:45:02 +0800 Subject: [PATCH] update: May20 find longest awesome substring [H] --- daily/May20.cpp | 28 ++++++++++++++++++ .../1542_find_longest_awesome_substring.cpp | 29 +++++++++++++++++++ .../1542_find_longest_awesome_substring.py | 0 .../1542_find_longest_awesome_substring.rs | 29 +++++++++++++++++++ 4 files changed, 86 insertions(+) create mode 100644 daily/May20.cpp create mode 100644 daily/cpp/1542_find_longest_awesome_substring.cpp create mode 100644 daily/python/1542_find_longest_awesome_substring.py create mode 100644 daily/rust/1542_find_longest_awesome_substring.rs diff --git a/daily/May20.cpp b/daily/May20.cpp new file mode 100644 index 0000000..c8b5631 --- /dev/null +++ b/daily/May20.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +class Solution { + public: + int longestAwesome(string s) { + int n = s.size(); + unordered_map prefix = {{0, -1}}; + int ans = 0; + int sequence = 0; + for (int i = 0; i < n; i++) { + int digit = s[i] - '0'; + sequence ^= (1 << digit); + if (prefix.count(sequence)) { + ans = max(ans, i - prefix[sequence]); + } else { + prefix[sequence] = i; + } + for (int k = 0; k < 10; k++) { + if (prefix.count(sequence ^ (1 << k))) { + ans = max(ans, i - prefix[sequence ^ (1 << k)]); + } + } + } + + return ans; + } +}; \ No newline at end of file diff --git a/daily/cpp/1542_find_longest_awesome_substring.cpp b/daily/cpp/1542_find_longest_awesome_substring.cpp new file mode 100644 index 0000000..366b120 --- /dev/null +++ b/daily/cpp/1542_find_longest_awesome_substring.cpp @@ -0,0 +1,29 @@ +#include +using namespace std; + +class Solution { + public: + int longestAwesome(string s) { + int n = s.size(); + unordered_map prefix = {{0, -1}}; + int ans = 0; + int sequence = 0; + for (int i = 0; i < n; i++) { + int digit = s[i] - '0'; + sequence ^= (1 << digit); + if (prefix.count(sequence)) { + ans = max(ans, i - prefix[sequence]); + } else { + prefix[sequence] = i; + } + + for (int k = 0; k < 10; k++) { + if (prefix.count(sequence ^ (1 << k))) { + ans = max(ans, i - prefix[sequence ^ (1 << k)]); + } + } + } + + return ans; + } +}; diff --git a/daily/python/1542_find_longest_awesome_substring.py b/daily/python/1542_find_longest_awesome_substring.py new file mode 100644 index 0000000..e69de29 diff --git a/daily/rust/1542_find_longest_awesome_substring.rs b/daily/rust/1542_find_longest_awesome_substring.rs new file mode 100644 index 0000000..b84e9b2 --- /dev/null +++ b/daily/rust/1542_find_longest_awesome_substring.rs @@ -0,0 +1,29 @@ +use std::collections::HashMap; + +impl Solution { + /// Time: O(n|\Sigma|), n: len(s), \Sigma: charset, 10 chars + /// Space: O(min{n, 2^{|\Sigma|}}), the space that hash mapping + pub fn longest_awesome(s: String) -> i32 { + let mut prefix: HashMap = HashMap::new(); + prefix.insert(0, -1); + let mut ans = 0; + let mut sequence = 0; + for (i, ch) in s.chars().enumerate() { + let digit = ch.to_digit(10).unwrap() as i32; + sequence ^= (1 << digit); + if let Some(&prev_index) = prefix.get(&sequence) { + ans = ans.max(i as i32 - prev_index); + } else { + prefix.insert(sequence, i as i32); + } + + for k in 0..10 { + if let Some(&prev_index) = prefix.get(&(sequence ^ (1 << k))) { + ans = ans.max(i as i32 - prev_index); + } + } + } + + ans + } +}