您的位置:首页 > 产品设计 > UI/UE

unique-substrings-in-wraparound-string(好)

2016-12-10 22:33 204 查看
https://leetcode.com/problems/unique-substrings-in-wraparound-string/

好,我自己做出来的。多总结规律,多思考。

package com.company;

import java.util.HashMap;
import java.util.Map;

class Solution {
public int findSubstringInWraproundString(String p) {
Map<Integer, Integer> mp = new HashMap<>();
int ret = 0;
int cur = 0;
char[] array = p.toCharArray();
if (array.length < 1) {
return 0;
}
for (int i=0; i<26; i++) {
mp.put(i, 0);
}

ret++;
cur++;
mp.put(array[0]-'a', 1);
for (int i=1; i<array.length; i++) {
if ((array[i]-array[i-1]+26) % 26 == 1)  {
cur++;
}
else {
cur = 1;
}

if (cur > mp.get(array[i]-'a')) {
ret += cur - mp.get(array[i]-'a');
mp.put(array[i]-'a', cur);
}
}
return ret;
}
}

public class Main {

public static void main(String[] args) throws InterruptedException {

String p = "zab";

Solution solution = new Solution();
int ret  = solution.findSubstringInWraproundString(p);

// Your Codec object will be instantiated and called as such:
System.out.printf("ret:%d\n", ret);

System.out.println();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: