share my java solution! beat 96%!


  • 0
    T
    public class Solution {
        public int findSubstringInWraproundString(String p) {
            char[] array=p.toCharArray();
            int[] dp=new int[26];
            
            int sum=0;
            for(int i=0;i<array.length;i++){
                int count=1;
                dp[array[i]-'a']=Math.max(dp[array[i]-'a'],1);
                
                while(i+1<array.length && (array[i+1]+26-array[i])%26==1){
                    count+=1;
                    dp[array[i+1]-'a']=Math.max(dp[array[i+1]-'a'],count);
                    i++;
                }
            }
            
            for(int i=0;i<dp.length;i++){
                sum+=dp[i];
            }
            
            return sum;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.