Skip to content

Commit 64944da

Browse files
refactor 467
1 parent 5b03c82 commit 64944da

File tree

2 files changed

+67
-40
lines changed

2 files changed

+67
-40
lines changed
Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,65 @@
11
package com.fishercoder.solutions;
22

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.
812
913
Example 1:
1014
Input: "a"
1115
Output: 1
12-
1316
Explanation: Only the substring "a" of string "a" is in the string s.
17+
1418
Example 2:
1519
Input: "cac"
1620
Output: 2
1721
Explanation: There are two substrings "a", "c" of string "cac" in the string s.
22+
1823
Example 3:
1924
Input: "zab"
2025
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+
*/
2228
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']);
4556
}
46-
dp[p.charAt(i) - 'a'] = Math.max(len, dp[p.charAt(i) - 'a']);
47-
}
4857

49-
for (int i : dp) {
50-
count += i;
58+
for (int i : dp) {
59+
count += i;
60+
}
61+
return count;
5162
}
52-
return count;
5363
}
5464

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-
}
6165
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._467;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static junit.framework.Assert.assertEquals;
8+
9+
public class _467Test {
10+
private static _467.Solution1 solution1;
11+
12+
@BeforeClass
13+
public static void setup() {
14+
solution1 = new _467.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(1, solution1.findSubstringInWraproundString("a"));
20+
21+
}
22+
23+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy