Return the number of times the string "hello/Hello/...etc" appears anywhere in the given string.
The different in the problem is that
The string hello can be in any case ie either upper case or lower case.
Sample Input #1
count("abc hello def")
Sample Output #1
1
Sample Input #2
count("Hi. Hello. Hello. Ok")
Sample Output #2
2
Sample Input #3
count("hi")
Sample Output #3
0
MyApproach
public int count(String str)
{
String str1="Hello";
int l=str.length();
int l1=str1.length();
if(l<l1)
{
return 0;
}
else
{
int count=0;
int p=0;
int j=0;
while(j<l)
{
char c=str.charAt(j);
char c1=str1.charAt(p);
if(c==c1)
{
p++;
if(p==l1)
{
count++;
p=0;
}
}
else
{
p=0;
}
j++;
}
return count;
}
}
Output TestcaseParameters Testcase Actual Answer Expected
No output 'HELLO how are you' 0 1
I am getting the following output.
Can anyone tell me what I am doing wrong?
I'm wondering how your code compiles. c1 is not even declared. I would probably use such logic:
str1.toUpperCase().split(str2.toUpperCase).length()-1
The code above does not return correct answer when str1
beginsWith or endsWith str2
. Here is a better version:
public static int count(String str1, String str2) {
int count = 0;
int len1 = str1.length();
int len2 = str2.length();
for (int i=0;i<=len1-len2;++i){
if ((str1.substring(i,i+len2)).equalsIgnoreCase(str2)) {
++count;
}
}
return count;
}
The code moves str2
rightwards once a step, and compares if str2
is the same as the substring of str1
. Please note in the situation str1
= "AAA", and str2
= "AA", the result is 2, since both the first and the second occurrences of AA
will have a match.
Step 1:
str1: *****
str2: **
Step 2:
str1: *****
str2: **
Step 3:
str1: *****
str2: **
Step 4:
str1: *****
str2: **
If you wish in the above situation, AA
only counts once, here is the code for you:
public static int count(String str1, String str2) {
int count = 0;
int len1 = str1.length();
int len2 = str2.length();
for (int i=0;i<=len1-len2;){
if ((str1.substring(i,i+len2)).equalsIgnoreCase(str2)) {
++count;
i+=len2;
}else{
++i;
}
}
return count;
}
This version above works this way:
Step 1:
str1: *****
str2: **
Step 2:
str1: *****
str2: **
It will not compare any character already compared in the previous steps.
I tested the code against your sample input data, and they all return the expected results.
If this is a school assignment then pls say so. Otherwise You will need to ask a question on google like: 'count occurrences in string java' or something. This will give you - tada - stackoverflow:
Occurrences of substring in a string
Hope to be of help.
Here is a pretty intuitive solution :
public static void main(String[] args) {
System.out.println(count("hellohellohellohihihihihihihellohihhihello")); // Prints : 5
}
public static int count(String str){
String str1 = "hello";
String str2 = str.toLowerCase();
int index = 0;
int count = 0;
while (true){
if (str2.contains(str1)){
count++;
str2 = str2.substring(str2.indexOf(str1) + str1.length(), str2.length());
} else {
return count;
}
}
}
//in this we have changed every upper case character in lowercase for comparing it with s2 public int count(String str){ String s1="";String s2="hello"; int l2=s2.length(); int count=0;
int l1=str.length();
for(int i=0;i<=l1-l2;i++){
for(int j=i;j<l2+i;j++){
char ch=str.charAt(j);
if(ch>=65&&ch<=90){
ch=(char)(ch+32);
s1=s1+ch;
}
else
s1=s1+ch;
}
if(s1.equals(s2)){
count++;
}
//in that line i have removed the data of string s1 to form new string again
s1="";
}return count;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.