[LeetCode]467. Unique Substrings in Wraparound String

来源:互联网 发布:浙江卫视网络直播源 编辑:程序博客网 时间:2024/05/17 01:26

https://leetcode.com/problems/unique-substrings-in-wraparound-string/



int[26]记录以每个字符为结尾的最大子字符串长度,最后求和该数组即为所求

public class Solution {    public int findSubstringInWraproundString(String s) {    if (s == null || s.length() == 0) {    return 0;    }    int[] cache = new int[26];    int count = 1;    int res = 0;    cache[s.charAt(0) - 'a'] = 1;    for (int i = 0; i < s.length() - 1; i++) {    if (s.charAt(i + 1) - s.charAt(i) == 1 || (s.charAt(i) - s.charAt(i + 1) == 25 && s.charAt(i)     == 'z')) {    count++;    } else {    count = 1;    }    cache[s.charAt(i + 1) - 'a'] = Math.max(cache[s.charAt(i + 1) - 'a'], count);    }    for (int num : cache) {    res += num;    }    return res;    }}


0 0
原创粉丝点击