Lonely Y loves his string of lowercase letters, Y is about to escape so he should choose just one lowercase letter and carry all letters in which is equal to 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<Character, Integer> map = new HashMap<>();for (int i = 0; i < len; i++) {map.put(input[i], map.getOrDefault(input[i], 0) + 1);}List<Integer> ls = new ArrayList<>();for (Map.Entry<Character, Integer> entry : map.entrySet()) {ls.add(entry.getValue());}Collections.sort(ls);System.out.println(ls.get(ls.size() - 1));}}
No comments:
Post a Comment