Java Program for String compression | Amazon Coding Interview Questions

preview_player
Показать описание


(For all updates on Java interview questions and java tutorials join the telegram group)
Search in Telegram with text Java Tutorials if you are unable to join telegram with above link

1) How to read a matrix from console in java?

2) How to remove duplicates from string in java?

3) Write a java program to duplicate characters with number of occurences in java?

4) Write a java program to implement bubble sort part 1?

5) Write a java program to check string is palindrome or not?

6) Write a java program to print floyds triangle?

7) Write a java program to print floyds triangle?





8) How to count number of vowels in a string in java?

9) How to find largest and smallest values in an array in java?

10) Write java program to find duplicate elements in array in java?

11) write a java program to check the input number is prime or not in java?

12) Write a java program to print pyramid pattern of numbers?

13) Write a java program to find missing number in array in java?

14)Write a java program to print numbers in java up to 10 with out using loops?

15) How to sort elements in array in asc and desc order in java?



16) Write a java program to find factorial of a number in java?

17) write a java program to find second largest element in the array in the java?

18) Write a java program to check two Strings are anagrams or not by sorting and comparing strings?

19) Write java program to print Characters count in each word in String ?

20) Write a java program to find reverse of a string in java?

21) Write a java program to add elements in an array in java?

22) Write a java program to find largest of three numbers?



23) Write a java program to print multiplication table?

24) Write a java program to print star pattern?

25) Write a java program to find factors of number from 1 to 100

26) Write a java program to print fibonacci series using loops?

27) Write a java program to print Ascii values of a Character ?

Рекомендации по теме
Комментарии
Автор

My approach is :
String str = "aabbcccdd";
String result = "";
int[] freq = new int[26];
for(int i = 0; i < str.length();i++)
{
freq[str.charAt(i)-'a']++;
}

for(int i = 0; i < str.length(); i++)
{
if(freq[str.charAt(i) -'a'] != 0)
{
result += str.charAt(i);
result += freq[str.charAt(i)-'a'];
freq[str.charAt(i)-'a'] = 0;
}
}

Inspiring_minds_
Автор

Sir please make video's continuously
Your teaching style is really very easy any person easily understand the Concept

thebaseofpcmbclasses
Автор

/* Amazon Question:
* Given a string write a function to compress it by shortening every sequence of the same character
* followed by number of repetitions. If you compressed string longer than the original,
* you should return the original string.
* eg:
* compress("a") = a
* compress("aaa")=a3
* compress("aaabbb")= a3b3
* compress("aaabccc") = a3b1c3
*
* */

public class StringCompress2 {

public static void main(String[] args) {





}

private static String compress(String str) {
String out = "";
int sum = 1;
for (int i = 0; i < str.length() - 1; i++) {

if (str.charAt(i) == str.charAt(i + 1)) {
sum++;
} else {
out = out + str.charAt(i) + sum;
sum = 1;
}
}
out = out + str.charAt(str.length() - 1) + sum;
return out.length() < str.length() ? out : str;

}

}

ganeshit
Автор

Sir as in question it is mentioned that output string length must me less than the input string ...but I think your code is failed in that case when we consider a string any string in which all the characters are unique for ex if we take a case in which input string is "plus" initially string length is 4 now after compression output is "p1l1u1s1" now string length is increased from 4 to 8 ...correct me if I am wrong

arishashukla
Автор

Sir how to write a program input is aaabbbcca output is 3a3b2c1a

anilsn
Автор

From where i can get amazon interview questions on LinkedIn?

Knightkder
Автор

String str= "ramakrishna";
Map<Character, Integer> count= new HashMap<>();
For(Character c:str.toCharacterArray)
curlbracked
If(count.containskey(c)) curlbracked
count. Put(c, count. get(c)+1);curlbracked
else
curlbracked
Count. Put(c, 1);
curlbracked
curlbracked
System. Out.println(count):
curlbracked

RamaKrishna-veqe
Автор

public class Program
{
static void Main(string[] args)
{
string str = "sandeep";
Dictionary<char, int> dict = new Dictionary<char, int>();
for(int i = 0; i < str.Length; i++)
{
if (dict.ContainsKey(str[i]))
{
dict[str[i]]++;
}
else
{
dict.Add(str[i], 1);
}
}
foreach(var x in dict)
{
Console.WriteLine("Key = "+x.Key + " Value = "+x.Value);
}
}
}

sainath