A thunder interview question: Find the number of times each letter appears in a string, the question string
By Long Luo
Personal blog address
Last time inThunder
During the interview, I encountered an algorithm question:
There is a long and long string, all composed of uppercase letters. You must find the number of times each letter appears in this string. Methods in STL are not allowed.
When I got this question, I first thought of the following methods:
In fact, it is relatively simple to review this question later, but unfortunately, it was not answered within the specified time, resulting in failure to pass the interview and get an Offer, record this question here.
Answer this question again.
How?
In fact, this question has a very simple solution to prevent, create an array, the size is 26, each character in the string is'A'
The result is that each letter is in the array.Element Base Value
The last array we get is the number of times each letter appears in this string.
Based on the above analysis, you can write the following code:
Package com. algorithm. alphabetSort;/*** there is a long string, which contains some letters. Calculate the number of occurrences of each letter (case sensitive ). ** @ Author luolong * @ date: 00:54:17 **/public class AlphabetSort {private static String str = "empty"; private static int [] outArray = new int [26]; public static void main (String [] args) {AlphabetSortString (str); printSortArray (outArray);} public static void AlphabetSortString (String str) {char cTemp; for (int I = 0; I <str. length (); I ++) {cTemp = str. charAt (I); outArray [cTemp-'a'] ++ ;}} private static void printSortArray (int [] arr) {for (int I = 0; I <arr. length; I ++) {char c = (char) ('A' + I); System. out. print ("" + c + "=" + arr [I] + "");}}}
Extension and extension 1
If a string is not only a capital letter, but a capital letter, how should it be written?
It is actually quite simple, as shown below:
If (cTemp> = 'A' & cTemp <= 'Z') {/*** uppercase letter */outArray [cTemp-'a'] ++ ;} else if (cTemp> = 'A' & cTemp <= 'Z ') {/*** lowercase letter */outArray [cTemp-'A' + 26] ++;} else {}
Extension and Extension 2
If the string isAll characters
Are statistics required?
What should we do?
Later, I found that it is also possible to store with HashMap, and this problem can be solved perfectly. The Code is as follows:
Package com. algorithm. alphabetSort; import java. util. arrayList; import java. util. collections; import java. util. hashMap; import java. util. list; import java. util. map;/*** there is a long string containing some letters. Calculate the number of occurrences of each letter (case sensitive ). ** @ Author luolong * @ date: 01:03:38 **/public class CharSort {private static String str = "empty"; public static void main (String args []) {Map <Character, KeyValue> map = new HashMap <Character, KeyValue> (); char c; KeyValue kv = null; for (int I = 0; I <str. length (); I ++) {c = str. charAt (I); kv = map. get (c); if (kv = null) {kv = new KeyValue (); kv. ch = c; kv. count = 1; map. put (c, kv);} else {kv. count ++ ;}list <KeyValue> List = new ArrayList <KeyValue> (map. values (); Collections. sort (list); for (KeyValue o: list) {System. out. print (o. ch + "=" + o. count + "") ;}} class KeyValue implements Comparable {public int compareTo (Object obj) {if (obj instanceof KeyValue) {KeyValue kv = (KeyValue) obj; return kv. count-this. count;} return-1;} char ch; int count ;}
Above.
If you have other better methods, please join us :-)
Created by Long Luo at 01:08:20 @ Shenzhen, China.
Completed By Long Luo at 01:28:39 @ Shenzhen, China.