This question already has answers here:
Java compressing Strings
(21 answers)
Closed 7 years ago.
I have a String and I need to count occurances of successive characters of the string.
String s1="aabbcccaaa";
It should print output as
a2b2c3a3
String s1="aabbcccaaa";
char c[]=s1.toCharArray();
StringBuffer sb=new StringBuffer();
LinkedHashMap<Character,Integer> map=new LinkedHashMap<Character,Integer>();
for(int i=0;i<c.length;i++)
{
Character c1=c[i];
Integer frequency=map.get(c1);
map.put(c1, (frequency==null)?1:frequency+1);
if(map.size()>1 && c[i]!=c[i-1])
{
sb.append(c[i-1]+""+map.get(c[i-1]));
map.remove(c[i-1]);
}
}
sb.append(map);
System.out.println(sb);
I am not sure how i can achieve counts if same elements occur again.
Hint #1: Test the current character against the previous one.
Hint #2: Ask yourself, why do you need to use a Map?
Related
This question already has answers here:
How do I count the number of occurrences of a char in a String?
(48 answers)
Closed 5 years ago.
I'm new to java and I cant find the answer to this problem.
If I have two character arrays like this with counters:
Character[] abc = {'A','B','C'};
int countABC = 0;
Character[] def = {'D','E','F'};
int countDEF = 0;
And a string like this:
String something = "ABCDEFGHABAB";
How to increase the counters?
If you loop through each of the characters in the string and then within the loop use 2 'if' functions, one for each array then you can use the indexOf() function to search an array for that value. If the value is negative then it wasn't found and you don't increase the counter.
Here is some more info on indexOf():
https://www.tutorialspoint.com/java/java_string_indexof.htm
This question already has answers here:
How to get the substring that contains the first N unicode characters in Java
(2 answers)
Closed 5 years ago.
I have a String that could contain 4 bytes characters. For example:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
I also have a size that I should use to get a substring from it. The size is in characters. So let's say that size is 5, so I should get the first 4 bytes character along with "1234".
Directly using substring as s.substring(0, 5) gives the wrong result returning the first character and just "123".
I could manage to get the right result using code points this way:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
StringBuffer buf = new StringBuffer();
long size = 5;
s.codePoints().forEachOrdered(charInt -> {
if(buf.codePoints().count() < size) {
buf.appendCodePoint(charInt);
}
});
I bet there should be a way better and more efficient code to achieve this.
You can use offsetByCodePoints in order to help find the index of the character following 5 code points, and then use that as the second parameter to substring:
String s = "\uD83D\uDC4D1234\uD83D\uDC4D";
String sub = s.substring(0, s.offsetByCodePoints(0, 5));
Ideone Demo
This question already has answers here:
Find longest common prefix?
(12 answers)
Closed 6 years ago.
Write a function to find the longest prefix of a list of string. For example,
['abc', 'abcde', 'abxyz'] => 'ab'
So it is an Arraylist and we find the longest prefix in the list of strings.
Let's try Java.
Please, no complete solutions
public string prefix (Arraylist<String> lst){
Arraylist<char[]> charLst = new Arraylist<>;
for(int i =0; i < lst.size(); i++){
charLst.add(lst.get(i).toCharArray());
}
}
But how do I proceed after creating a CharArray? This is already starting to be inefficient as it is O(n) with just the conversion to CharArray. I would just like some hint/help in the approach
Why do you add the ith character of every string to charLst? What you need is just the length of the longest common prefix, and then you can output the prefix based on the length you got.
This question already has answers here:
What's the best way to check if a character is a vowel in Java?
(5 answers)
Closed 6 years ago.
I was trying to grab the first character from user entry, and determine it to be a vowel or not. I am very new, and have been struggling with this for a while. I am trying to add all vowels to the variable 'vowel', and not only will that obviously not work, but I feel like I am going the long way. Any help at all is vastly appreciated as I am very new to this.
entry = scanner.nextLine();
letters = entry.substring(0,1);
holder = entry.substring(1);
vowels = "A";
if (entry.substring(0,1).equals(vowels)) {
pigLatinVowel = entry + "way";
System.out.println(pigLatinVowel);
}
First, since you only want one character, don't use string methods, i.e. use entry.charAt(0) == 'A' instead of entry.substring(0,1).equals("A").
With that, you can then turn it around:
"AEIOU".indexOf(entry.charAt(0))
If the character at position 0 in the entry variable can be found, indexOf() returns the index position (zero-based), otherwise it returns -1.
So, to see if it is a vowel, do this:
if ("AEIOU".indexOf(entry.charAt(0)) != -1) {
pigLatinVowel = entry + "way";
System.out.println(pigLatinVowel);
}
This question already has answers here:
How do I count the number of occurrences of a char in a String?
(48 answers)
Closed 6 years ago.
I have below mentioned string
String str = "\nArticle\n\nArticle\nArticle";
I want total number of count. How can i get this?
As the string contain \n so always it gives 1 instead of 3
To get you started, I will show you a simple example:
String str = "\nArticle\n\nArticle\nArticle";
// Split the String by \n
String[] words = str.split("\n");
// Keep the count of words
int wordCount = 0;
for(String word : words){
// Only count non-empty Strings
if(!word.isEmpty()) {
wordCount++;
}
}
// Check, answer is 3
System.out.println(wordCount);