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(); } }
时间: 2024-12-25 00:30:51