java - Code with minimum time complexity to find occurrence of max and min Character in a String -
i have find best possible solution find occurrence of min , max character in given string.
code using below
string prefix="aaaaabbbbddddfeeeee" hashmap<character, integer> alphabetcountmapinit = new hashmap<character, integer>(); //time complexity : o(n) (char c : prefix.tochararray()) { alphabetcountmapinit.put(c, alphabetcountmapinit.get(c) == null ? 1 : alphabetcountmapinit.get(c) + 1); } //time complexity : o(n) int minoccurence = collections.min(alphabetcountmapinit.values()); system.out.println(maxoccurance); //time complexity : o(n) int maxoccurence = collections.max(alphabetcountmapinit.values()); system.out.println(minoccurance);
`
can suggest me solution more optimized in terms of time complexity, need iterate around 90^90 number of string records.?
you don't have use hashmap. there 26 letters, can use int[] count = new int[26];
count each letter. when have 10 a
s, set count['a' - 97] = 10
count[0] = 10
. , @ same time, use int min = 0, max = 0;
to keep track of min , max when iterate string , write count
array. o(n)
time , o(1)
space.
Comments
Post a Comment