Amazon Interview Question
Quality Assurance EngineersTeam: QAE 1
Country: India
Interview Type: In-Person
THe easiest way is to use integer array with length 256 and update the value as we enounter each character.
int [] array = new int[256]
char charArray[] = str.toCharArray();
foreach(char c in charArray)
{
array[c]++;
}
This is the classic solution from the times when a character was 8 bits. These days you should consider Unicode, too.
package com.amazon.techinterview;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
public class NoOfChars {
public static void main (String args[]){
Hashtable<Character, Integer> myHash = new Hashtable<Character, Integer>(26);
String myString = new String("gbvsyuxfasyusdfaxsasghdvsgcydegfrdywegvfwehyicv");
int length = myString.length();
for (int i=0; i<length; i++){
char ch = myString.charAt(i);
if (!myHash.containsKey(ch)){
myHash.put(ch,1);
}
else {
int presentCount = myHash.get(ch);
myHash.remove(ch);
myHash.put(ch, presentCount + 1);
}
}
Set<Entry<Character, Integer>> mySet = myHash.entrySet();
Iterator it = mySet.iterator();
while (it.hasNext()){
Map.Entry entry = (Map.Entry) it.next();
System.out.println(entry.getKey() + " : " + entry.getValue());
}
}
}
int numberOfOccuranceOfPattern(char * str, char * pattern)
{
int numOcc = 0;
int i = 0;
int j = 0;
while(str[i] != '\0')
{
if(pattern[j] == str[i])
{
while(pattern[j] != '\0')
{
if(pattern[j] != str[i])
{
j = 0;
break;
}
j++;
i++;
if((pattern[j] == '\0') && ((str[i] == ' ') || (str[i] == '\0')))
{
numOcc++;
j = 0;
}
}
}
else
{
i++;
}
}
return numOcc;
}
import java.util.*;
public class B {
public static void main(String[] args) throws Exception{
Map<Character, Integer> map = new TreeMap<Character, Integer>();
String input = "Varun Saxena";
for (int i = 0; i < input.length(); i++) {
char in = input.charAt(i);
if(in == ' ' )continue;
if(map.get(in) != null){
int count = map.get(in);
map.put(in, ++count);
}else {
map.put(input.charAt(i), 1);
}
}
System.out.print(map);
}
}
import java.util.Hashtable;
public class CountCharOcurrence {
public static void main(String[] args) {
String word = "PROGRAM";
char[] charArray = word.toCharArray();
Hashtable<Character, Integer> hashtable = new Hashtable<Character, Integer>();
for (char c : charArray) {
System.out.println(c);
if (hashtable.get(c) != null) {
hashtable.put(c, hashtable.get(c) + 1);
} else {
hashtable.put(c, 1);
}
}
System.out.println(hashtable.entrySet());
}
}
This is my approach.
please see my approach.
String name="rnagabandi";
HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
int temp=0;
hashMap.put(name.charAt(0),1);
for (int i = 1; i < name.length(); i++) {
if(hashMap.get(name.charAt(i))!=null){
temp=hashMap.get(name.charAt(i));
hashMap.put(name.charAt(i),temp+1);
}else{
hashMap.put(name.charAt(i),1);
}
temp=0;
}
System.out.println(hashMap);
public static void main(String args[]){
String sat = "test";
sat =sat.toUpperCase();
int num = 0;
char[] sat1=sat.toCharArray();
for(int j=0;j<sat1.length;j++){
int count = 0;
for(int i=0;i<sat1.length;i++){
// System.out.print(sat1[i] + " ");
if(sat1[num]==sat1[i]){
count = count +1;
}
}
System.out.println(sat1[num] + " " + count);
num=num+1;
}
}
# include <stdio.h>
# include <conio.h>
void main ()
{
int i,j,arr[256],size;
char *text;
printf("Enter the text : ");
scanf("%s",text);
i = 0;
for (i=0;i<255;i++)
{
arr[i]=0;
}
i=0;
while(text[i] != '\0')
{
arr[(int)text[i]]++;
i++;
}
for(i=0;i<255;i++)
{
if( arr[i]>0)
{
printf("%c : %d\n",(char)i,arr[i]);
}
}
printf("\n%s",text);
getch ();
clrscr();
}
public class CountCharOccurance
{
public static void main(String[] args)
{
String value = "Test@Tutor";
int[] count = new int[256];
value = value.toLowerCase();
for (int i = 0; i< value.length(); i++)
{
count[(int)value.charAt(i)]++;
}
List<Character> printed = new ArrayList<Character>();
for (int i = 0; i< value.length(); i++)
{
if (printed.contains(value.charAt(i)))
continue;
printed.add(value.charAt(i));
System.out.println("Occurrence of char [" + value.charAt(i) + "] - " + count[(int)value.charAt(i)]);
}
}
}
Use a hash table.. loop through the char array and for each char insert the character as key and count as the value...
- VVS February 13, 2012