Why internal and interval in example is not abbreviated


  • 0
    D

    As the title explains, I code as per my understanding and getting the output as ["l2e","god","i6l","me","i6t","in5l","i7n","f2e","in6n"]
    The code is as below.

    public class Solution {
        public IList<string> WordsAbbreviation(IList<string> dict) {
            List<string> result = new List<string>();
            foreach(string word in dict) {
                result.Add(GetAbbreviation(word));
            }
            return result;
        }
        
        private string GetAbbreviation(string word) {
            if(word.Length <= 3)
                return word;
            
            int maxPrefixLength = word.Length - 2;
            for(int i = 1; i < maxPrefixLength; ++i) {
                string wordAbbr = this.GetAbbreviation(word, i);
                if(!hashSet.Contains(wordAbbr)) {
                    hashSet.Add(wordAbbr);
                    return wordAbbr;
                }
            }
            // cant abbreviate the word, adding as it is
            hashSet.Add(word);
            return word;
        }
        
        private string GetAbbreviation(string word, int prefixLength) {
           string pre = word.Substring(0,prefixLength);
           return pre +  (word.Length-prefixLength - 1) + word[word.Length-1];
        }
        
        private HashSet<string> hashSet = new HashSet<string>();
    }
    

Log in to reply
 

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