Amazon Interview - Check string is multiple duplicate - 我的博客 - ITeye技术网站
有一种String,是把一个更短的String重复n次而构成的,那个更短的String长度至少为2,输入一个String写代码返回true或者false
例子:
"abcabcabc" true 因为它把abc重复3次构成
"bcdbcdbcde" false 最后一个是bcde
"abcdabcd" True 因为它是abcd重复2次构成
"xyz" false 因为它不是某一个String重复
"aaaaaaaaaa" false 重复的短String长度应至少为2(这里不能看做aa重复5次)
要求算法复杂度为O(n)
Solution:
patternPos表示从哪里开始匹配。初始值为0。
patternEnd表示模式结束的位置。初始值为0。
当前字符和patternPos不相等的话,说明pattern不成立,patternPos回到开始位置0,patternEnd设为当前字符所在位置i。
当前字符和patternPos相等的话,patternPos加1移到下一个位置,patternEnd保持不变。并检查下如果patternPos超过patternEnd的话,说明截止到当前字符成功匹配,patternPos回到开始开始匹配下一串字符。注意,当i走到最后的话,patternPos就不用清0了。
最后判断成功的条件是,patternPos大于patternEnd的时候,说明整个字符串pattern且至少重复pattern两次。patternEnd大于零,说明pattern长度大于1。
Read full article from Amazon Interview - Check string is multiple duplicate - 我的博客 - ITeye技术网站
有一种String,是把一个更短的String重复n次而构成的,那个更短的String长度至少为2,输入一个String写代码返回true或者false
例子:
"abcabcabc" true 因为它把abc重复3次构成
"bcdbcdbcde" false 最后一个是bcde
"abcdabcd" True 因为它是abcd重复2次构成
"xyz" false 因为它不是某一个String重复
"aaaaaaaaaa" false 重复的短String长度应至少为2(这里不能看做aa重复5次)
要求算法复杂度为O(n)
Solution:
patternPos表示从哪里开始匹配。初始值为0。
patternEnd表示模式结束的位置。初始值为0。
当前字符和patternPos不相等的话,说明pattern不成立,patternPos回到开始位置0,patternEnd设为当前字符所在位置i。
当前字符和patternPos相等的话,patternPos加1移到下一个位置,patternEnd保持不变。并检查下如果patternPos超过patternEnd的话,说明截止到当前字符成功匹配,patternPos回到开始开始匹配下一串字符。注意,当i走到最后的话,patternPos就不用清0了。
最后判断成功的条件是,patternPos大于patternEnd的时候,说明整个字符串pattern且至少重复pattern两次。patternEnd大于零,说明pattern长度大于1。
- public boolean isMultipleDuplicate(String s) {
- int patternPos = 0, patternEnd = 0;
- for(int i=1; i<s.length(); i++) {
- if(s.charAt(i) != s.charAt(patternPos)) {
- patternPos = 0;
- patternEnd = i;
- } else {
- if(++patternPos > patternEnd && i != s.length()-1) {
- patternPos = 0;
- }
- }
- }
- return patternPos>patternEnd && patternEnd>0;
- }
另外,上面的代码还可以加判断:patternEnd > s.length() / 2,走过一半就可以结束了;
以及 s.length() % (patternEnd + 1) != 0, ++i, ++patternEnd,过滤掉长度不符合的情况。
以及 s.length() % (patternEnd + 1) != 0, ++i, ++patternEnd,过滤掉长度不符合的情况。
Applying the KMP algorithm,
given the example of ababab. First calculate the failure function according to the KMP algorithm
public
int
[] failure(String s) {
int
[] f =
new
int
[s.length() +
1
];
f[
0
] = f[
1
] =
0
;
int
j =
0
;
// record the last possible position to extend
for
(
int
i =
1
; i < f.length -
1
; i++) {
if
(s.charAt(i-
1
) == s.charAt(k)) {
f[i+
1
] = k+
1
;
k++;
}
else
if
(k !=
0
) {
k = f[k];
i--;
// try next k with same i
}
else
{
f[i+
1
] =
0
;
}
}
return
f;
}
The same method can be used to solve the string concatenation problem above. Assume we have a string of “ABABAB”, then if we get the string that is both prefix and suffix of the original string, and sort them according to the length. we get
1. ABAB
2. AB
3. empty string
1. ABAB
2. AB
3. empty string
Then according to these suffix/prefix string, we get another set of strings which, after being inserted in front of the string will ended up the original string responsively We call them “augmenting string”.
1.AB
2.ABAB
3.ABABAB
2.ABAB
3.ABABAB
Because each of these string will be the prefix of the original string, they are also part of the suffix string according to the prefix/suffix string. That means now the suffix/prefix contains at least two copies of the “augmenting” string as a prefix (since it’s also a prefix of the initial string) and so on. Of course the suffix/prefix under question needs to be long enough. In other words, the length of a successful “candidate” must divide with no remainder the length of the initial string.
So what we need to do is to check the augmenting string one by one from the shortest all the way to the longest (the original string), and if any of them can be divided by the length of the suffix/prefix string, it is the concatenating string. That is what the failure function was designed for. The failure function for S = “ABABAB” would be F = {0001234}
So what we need to do is simply follow the failure function from the tail n = 4;
So what we need to do is simply follow the failure function from the tail n = 4;
public
static
String minConcat(String s) {
int
[] f = failure(s);
int
n = f[s.length()];
String concast = s;
while
(n !=
0
) {
if
(n%(s.length() - n) ==
0
) {
//n should be greater thatn s.length() - n if not, the statement will not be 0.
concast = s.substring(
0
, s.length() - n);
}
n = f[n];
}
return
concast;
}