|
1 | 1 | package com.fishercoder.solutions; |
2 | 2 |
|
3 | | -/**Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". |
4 | | -
|
5 | | - Now we have another string p. Your job is to find out how many unique non-empty substrings of p are present in s. In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s. |
6 | | -
|
7 | | - Note: p consists of only lowercase English letters and the size of p might be over 10000. |
| 3 | +/** |
| 4 | + * 467. Unique Substrings in Wraparound String |
| 5 | + * |
| 6 | + * Consider the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", |
| 7 | + * so s will look like this: "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....". |
| 8 | + * Now we have another string p. |
| 9 | + * Your job is to find out how many unique non-empty substrings of p are present in s. |
| 10 | + * In particular, your input is the string p and you need to output the number of different non-empty substrings of p in the string s. |
| 11 | + * Note: p consists of only lowercase English letters and the size of p might be over 10000. |
8 | 12 |
|
9 | 13 | Example 1: |
10 | 14 | Input: "a" |
11 | 15 | Output: 1 |
12 | | -
|
13 | 16 | Explanation: Only the substring "a" of string "a" is in the string s. |
| 17 | +
|
14 | 18 | Example 2: |
15 | 19 | Input: "cac" |
16 | 20 | Output: 2 |
17 | 21 | Explanation: There are two substrings "a", "c" of string "cac" in the string s. |
| 22 | +
|
18 | 23 | Example 3: |
19 | 24 | Input: "zab" |
20 | 25 | Output: 6 |
21 | | - Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s.*/ |
| 26 | + Explanation: There are six substrings "z", "a", "b", "za", "ab", "zab" of string "zab" in the string s. |
| 27 | + */ |
22 | 28 | public class _467 { |
23 | | - /**A naive solution would definitely result in TLE. |
24 | | - * Since the pattern keeps repeating, DP is the way to go. |
25 | | - * Because the input consists merely of lowercase English letters, we could maintain an array of size 26, |
26 | | - * keep updating this array, counting the substrings that end with this letter, keep updating it with the largest one possible. |
27 | | - * |
28 | | - * Inspired by this: https://discuss.leetcode.com/topic/70658/concise-java-solution-using-dp*/ |
29 | | - public static int findSubstringInWraproundString(String p) { |
30 | | - if (p == null || p.isEmpty()) { |
31 | | - return 0; |
32 | | - } |
33 | | - if (p.length() < 2) { |
34 | | - return p.length(); |
35 | | - } |
36 | | - int count = 0; |
37 | | - int[] dp = new int[26]; |
38 | | - dp[p.charAt(0) - 'a'] = 1; |
39 | | - int len = 1; |
40 | | - for (int i = 1; i < p.length(); i++) { |
41 | | - if (p.charAt(i) - 1 == p.charAt(i - 1) || (p.charAt(i) == 'a' && p.charAt(i - 1) == 'z')) { |
42 | | - len++; |
43 | | - } else { |
44 | | - len = 1; |
| 29 | + public static class Solution1 { |
| 30 | + /** |
| 31 | + * A naive solution would definitely result in TLE. |
| 32 | + * Since the pattern keeps repeating, DP is the way to go. |
| 33 | + * Because the input consists merely of lowercase English letters, we could maintain an array of size 26, |
| 34 | + * keep updating this array, counting the substrings that end with this letter, keep updating it with the largest one possible. |
| 35 | + * <p> |
| 36 | + * Inspired by this: https://discuss.leetcode.com/topic/70658/concise-java-solution-using-dp |
| 37 | + */ |
| 38 | + public int findSubstringInWraproundString(String p) { |
| 39 | + if (p == null || p.isEmpty()) { |
| 40 | + return 0; |
| 41 | + } |
| 42 | + if (p.length() < 2) { |
| 43 | + return p.length(); |
| 44 | + } |
| 45 | + int count = 0; |
| 46 | + int[] dp = new int[26]; |
| 47 | + dp[p.charAt(0) - 'a'] = 1; |
| 48 | + int len = 1; |
| 49 | + for (int i = 1; i < p.length(); i++) { |
| 50 | + if (p.charAt(i) - 1 == p.charAt(i - 1) || (p.charAt(i) == 'a' && p.charAt(i - 1) == 'z')) { |
| 51 | + len++; |
| 52 | + } else { |
| 53 | + len = 1; |
| 54 | + } |
| 55 | + dp[p.charAt(i) - 'a'] = Math.max(len, dp[p.charAt(i) - 'a']); |
45 | 56 | } |
46 | | - dp[p.charAt(i) - 'a'] = Math.max(len, dp[p.charAt(i) - 'a']); |
47 | | - } |
48 | 57 |
|
49 | | - for (int i : dp) { |
50 | | - count += i; |
| 58 | + for (int i : dp) { |
| 59 | + count += i; |
| 60 | + } |
| 61 | + return count; |
51 | 62 | } |
52 | | - return count; |
53 | 63 | } |
54 | 64 |
|
55 | | - public static void main(String... args) { |
56 | | -// String p = "a"; |
57 | | -// String p = "abcgha"; |
58 | | - String p = "zab"; |
59 | | - System.out.println(findSubstringInWraproundString(p)); |
60 | | - } |
61 | 65 | } |
0 commit comments