Letter most

Lonely Y loves his string s of lowercase letters, Y is about to escape so he should choose just one lowercase letter c and carry all letters in s which is equal to c with himself.

What is the maximum number of letters Y can carry with himself?

Example:

Input:  len=7, str="vrowrqt"
Output: 2

Approach

Java


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LetterMost {
    public static void main(String args[]) {
        int len = 7;
        char[] input = "vrowrqt".toCharArray();

        Map<CharacterIntegermap = new HashMap<>();
        for (int i = 0; i < len; i++) {
            map.put(input[i], map.getOrDefault(input[i], 0) + 1);

        }

        List<Integerls = new ArrayList<>();
        for (Map.Entry<CharacterIntegerentry : map.entrySet()) {
            ls.add(entry.getValue());
        }
        Collections.sort(ls);

        System.out.println(ls.get(ls.size() - 1));
    }

}


No comments:

Post a Comment