Amazon Interview Question
Quality Assurance EngineersCountry: United States
private static void calculate(char[] charArray) {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (char c : charArray) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
StringBuilder sb = new StringBuilder();
for (Entry<Character, Integer> entry : map.entrySet()) {
sb.append(entry.getKey());
sb.append(entry.getValue());
}
System.out.println(sb.toString());
}
private static void calculate(char[] charArray) {
LinkedHashMap<Character, Integer> map = new LinkedHashMap<Character, Integer>();
for (char c : charArray) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
StringBuilder sb = new StringBuilder();
for (Entry<Character, Integer> entry : map.entrySet()) {
sb.append(entry.getKey());
sb.append(entry.getValue());
}
System.out.println(sb.toString());
}
/* The idea is to store characters against their frequencies in a Hash Table, and then to pull them out. Complexity: Space O(N), Time O(N). */
public static String writeRepetitions(String str){
int len = str.length();
if (len == 0) return str;
HashMap<Character,Integer> hm = new HashMap<>();
for (int i = 0; i<len; i++){
char ch = str.charAt(i);
if (hm.containsKey(ch)){
hm.put(ch, hm.get(ch)+1);
} else hm.put(ch, 1);
}
int j=0;
StringBuilder sb = new StringBuilder();
while (!hm.isEmpty()){
char c = str.charAt(j);
if (hm.containsKey(c)){
sb.append(c);
sb.append(hm.get(c));
hm.remove(c);
}
j++;
}
return sb.toString();
}
public class Solution {
public static void main(String[] args) {
String s = "abbaccdbac";
printOccurencesChars(s);
}
// assuming only ASCII chars in string.
private static void printOccurencesChars(String s) {
int[] count = new int[256];
for (int i = 0; i < count.length; i++) {
count[i] = 0;
}
char[] c = s.toCharArray();
int charVal = -1;
for (int i = 0; i < c.length; i++) {
charVal = (int) c[i];
count[charVal]++;
}
StringBuilder sb = new StringBuilder();
int charCount = -1;
for (int i = 0; i < count.length; i++) {
charCount = count[i];
if (charCount > 0) {
sb.append((char)i + "" + charCount);
}
}
System.out.println(sb.toString());
}
}
import java.util.Objects;
public class StringUtil {
// Assumed only ASCII character string
private static final int ASCII = 255;
public static String countChars(String str) {
if (Objects.isNull(str)) {
return str;
}
char[] chars = str.toCharArray();
int[] counter = new int[ASCII];
boolean[] isVisited = new boolean[ASCII];
for (char c : chars) {
counter[c]++;
}
StringBuilder builder = new StringBuilder();
for (char c : chars) {
if (isVisited[c]) {
continue;
}
isVisited[c] = true;
builder.append(c).append(counter[c]);
}
return builder.toString();
}
}
public class CountDuplicate {
public static void main(String[] args) {
String str = "abcdbababac";
int l =str.length();
int i=0;
String output="";
int count[] = new int[26];
if(l==0)
{
System.out.println("The string is empty");
System.exit(0);
}
for(i=0;i<l;i++)
{
count[str.charAt(i)-'a']++;
}
for(i=0;i<26;i++)
{
if(count[i]!=0)
output = output + (char)(i+'a') + count[i];
}
System.out.println(output);
}
}
public class DuplicateCharsInString {
public static void main(String[] args) {
String str = "aabbacklccl";
int[] count = new int[26];
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
int reminder = ((int) ch) % 97;
count[reminder] = count[reminder] + 1;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count.length; i++) {
if (count[i] > 0) {
int ch = 97 + i;
sb.append(Character.toString((char) ch)+count[i]);
}
}
System.out.println(sb.toString());
}
}
Hello
Thanks for the solution provided but while going through the code i am not able to understand the belo statement.
int reminder = ((int) ch) % 97;
it would be great if you can explain this?
String t="abbaccdbac";
Map<String, Integer> map = new HashMap<String, Integer>();
for(int i=0;i<t.length();i++)
{
String tmp = t.substring(i,i+1);
if(!map.containsKey(tmp))
{
map.put(tmp,1);
}
else
{
map.put(tmp, map.get(tmp)+1);
}
}
for(Entry<String, Integer> tm: map.entrySet())
{
System.out.println(tm.getKey()+"---"+tm.getValue());
}
String t="abbaccdbac";
Map<String, Integer> map = new HashMap<String, Integer>();
for(int i=0;i<t.length();i++)
{
String tmp = t.substring(i,i+1);
if(!map.containsKey(tmp))
{
map.put(tmp,1);
}
else
{
map.put(tmp, map.get(tmp)+1);
}
}
for(Entry<String, Integer> tm: map.entrySet())
{
System.out.println(tm.getKey()+"---"+tm.getValue());
}
public class CountDuplicateChars {
public static void main(String[] args) {
String str = "aabbacklccl";
int[] count = new int[26];
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
int reminder = ((int) ch) % 97;
count[reminder] = count[reminder] + 1;
}
StringBuffer sb = new StringBuffer();
for (int i = 0; i < count.length; i++) {
if (count[i] > 0) {
int ch = 97 + i;
sb.append(Character.toString((char) ch)+count[i]);
}
}
System.out.println(sb.toString());
}
}
Python 3:
def orderly_count(let_string):
mapping = {}
for item in let_string:
mapping[item] = mapping.get(item,0)+1
result = ''
for item in let_string:
if item not in result:
result += item + str(mapping[item])
return result
print(orderly_count('abbaccdbac')) #a3b3c3d1
print(orderly_count('bbddeeeaaccbbdef')) #b4d3e4a2c2f1
Simple Solution using HashMap:
public static void main(String[] args) {
// TODO Auto-generated method stub
String s="abbcaccbbaa";
char []charArray=s.toCharArray();
HashMap<Character, Integer> map = new HashMap<Character, Integer>();
for (char t : charArray) {
if (!map.containsKey(t)) {
map.put(t, 1);
} else {
map.put(t, map.get(t) + 1);
}
}
for (Map.Entry obj:map.entrySet()) {
System.out.print(obj.getKey() +""+obj.getValue());
}
}
Why do you have to make the solution so complicated?
public static String compressString (String str) {
if(str == null || str.length() <= 1) {
return str;
}
String compStr = "";
int num = 1;
char cc = str.charAt(0);
for(int cntr = 1; cntr < str.length(); cntr++) {
if(cc == str.charAt(cntr)) {
num ++;
} else {
compStr += cc + String.valueOf(num);
num = 1;
cc = str.charAt(cntr);
}
}
return compStr + cc + String.valueOf(num);
}
public class count_of_duplicateChar {
public static void main(String[] args) {
// Print the count of duplicate char in a given string in same order.
// Ex: Input- 'abbaccdbac', Output- 'a3b3c3d1'
String str = "abbaccdbac";
String[] myChar = str.split("");
int i = 0;
while (str.length()>0) {
int count = 0;
String repChar = myChar[i];
String finalChar="";
for (int j = 0; j <= myChar.length - 1; j++) {
if (repChar.equalsIgnoreCase(myChar[j])) {
count++;
}
}
str=str.replaceAll(repChar,"");
myChar =(str.split(""));
finalChar = repChar+count;
System.out.print(finalChar);
}
}
}
String val = "abbacca";
String a="a";String b="b";String c="c";
int acount=0;
int bcount=0;
int ccount=0;
for (int i=0; i<val.length(); i++)
{
if(val.charAt(i)=='a')
{
acount++;
System.out.println("Acount: "+acount);
}else if(val.charAt(i)=='b')
{
bcount++;
System.out.println("Bcount: "+bcount);
}else
{
ccount++;
System.out.println("Ccount: "+ccount);
}
System.out.println(a+acount+b+bcount+c+ccount);
}
String s="abbacca";
int count=0;
int len=s.length();
HashMap<Character,Integer> map=new HashMap<Character,Integer>();
for(int i=0;i<len;i++)
{
if(map.containsKey(s.charAt(i)))
{
count= map.get(s.charAt(i));
count++;
map.put(s.charAt(i), count);
}
else
{
map.put(s.charAt(i), 1);
}
}
for(Entry e:map.entrySet())
{
System.out.println(e.getKey()+":"+e.getValue());
}
public void getCount(String s){
char[] carr = s.toCharArray();
LinkedHashMap<Character,Integer> lmap = new LinkedHashMap<>();
for(Character c : carr){
if(lmap.containsKey(c)){
lmap.put(c, lmap.get(c)+1);
}
else{
lmap.put(c, 1);
}
}
for(Entry<Character, Integer> entry : lmap.entrySet()){
System.out.print(entry.getKey()+""+entry.getValue());
}
}
def charDup(string):
char = []
charCount = []
for i in range(len(string)):
if string[i] not in char:
char.append(string[i])
counter = 0
for j in range(len(string)):
if string[i] == string[j]:
counter = counter +1
counter = str(counter)
charCount.append(counter)
outputStr = ''
for k in range(len(char)):
outputStr = outputStr + char[k]+ charCount[k]
return (outputStr)
a = 'aabbddbbccaad'
print (charDup(a))
public void countDuplicates(char[] charArray) {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : charArray) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
for (char c:charArray) {
System.out.print(c+map.get(c));
}
}
Time Complexity :- O(n), Space Complexity: O(n)
Java solution, complexity is O(n):
- techinterviewquestion.com May 23, 2016