unique-substrings-in-wraparound-string(好)
Posted 笨鸟居士的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了unique-substrings-in-wraparound-string(好)相关的知识,希望对你有一定的参考价值。
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(); } }
以上是关于unique-substrings-in-wraparound-string(好)的主要内容,如果未能解决你的问题,请参考以下文章