why expect output of the "aba" is "a", but "bb" for "bb"


  • -1
    E
    import java.util.HashMap;
    import java.util.Map;
    
    public class Solution {
        public String longestPalindrome(String s) {
            Map<Character, Integer> start = new HashMap<>();
            Map<Character, Integer> end = new HashMap<>();
            for (int i=0;i<s.length(); i++){
                Character key = s.charAt(i);
                if (!start.containsKey(key)){
                    start.put(key,i);
                    end.put(key, i);
                }else {
                    end.put(key, i);
                }
            }
            int startIndex=0;
            int endIndex = 0;
            for (Map.Entry<Character, Integer> entry:start.entrySet()){
                Character key = entry.getKey();
                int startTmp = entry.getValue();
                int endTmp = end.get(key);
                if (endTmp-startTmp>endIndex-startIndex){
                    startIndex = startTmp;
                    endIndex = endTmp;
                }
            }
            return s.substring(startIndex,endIndex+1);
        }
    }
    

  • 0

    @Edaire Format your code properly posting a question next time.

    New users please read the instructions to format your code properly. Discuss is a place to post interview questions or share solutions / ask questions related to OJ problems. Thank you!


Log in to reply
 

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