Skip to content

Latest commit

 

History

History
80 lines (67 loc) · 2.01 KB

File metadata and controls

80 lines (67 loc) · 2.01 KB

467. Unique Substrings in Wraparound String

We define the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this:

  • "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".

Given a string p, return the number of unique non-empty substrings of p are present in s.

Example 1:

Input: p = "a"
Output: 1
Explanation: Only the substring "a" of p is in s.

Example 2:

Input: p = "cac"
Output: 2
Explanation: There are two substrings ("a", "c") of p in s.

Example 3:

Input: p = "zab"
Output: 6
Explanation: There are six substrings ("z", "a", "b", "za", "ab", and "zab") of p in s.

Constraints:

  • 1 <= p.length <= 105
  • p consists of lowercase English letters.

Solutions (Ruby)

1. Solution

# @param {String} p
# @return {Integer}
def find_substring_in_wrapround_string(p)
  p = p.bytes
  count = 1
  max_len = [0] * 26

  (0...p.size).each do |i|
    count = i == 0 || (p[i] + 26 - p[i - 1]) % 26 != 1 ? 1 : count + 1
    (0...[26, count].min).each do |j|
      k = p[i + 1 - count + j] - 97
      max_len[k] = [max_len[k], count - j].max
    end
  end

  max_len.sum
end

Solutions (Rust)

1. Solution

impl Solution {
    pub fn find_substring_in_wrapround_string(p: String) -> i32 {
        let p = p.as_bytes();
        let mut count = 1;
        let mut max_len = [0; 26];

        for i in 0..p.len() {
            if i == 0 || (p[i] + 26 - p[i - 1]) % 26 != 1 {
                count = 1;
            } else {
                count += 1;
            }
            for j in 0..26.min(count) {
                let k = (p[i + 1 - count + j] - b'a') as usize;
                max_len[k] = max_len[k].max(count - j);
            }
        }

        max_len.iter().sum::<usize>() as i32
    }
}