简体   繁体   English

返回字符串“hello/HELLO/hello...”出现在给定字符串中任意位置的次数

[英]Return the number of times the string "hello/HELLO/hellO ..." appears anywhere in the given string

Return the number of times the string "hello/Hello/...etc" appears anywhere in the given string.返回字符串“hello/Hello/...etc”出现在给定字符串中任意位置的次数。

The different in the problem is that问题的不同之处在于

The string hello can be in any case ie either upper case or lower case.字符串 hello 可以是任何情况,即大写或小写。

Sample Input #1样本输入 #1
count("abc hello def") count("abc 你好 def")

Sample Output #1示例输出 #1
1 1

Sample Input #2样本输入 #2
count("Hi. Hello. Hello. Ok") count("嗨。你好。你好。好的")

Sample Output #2示例输出 #2
2 2

Sample Input #3示例输入 #3
count("hi")计数(“嗨”)

Sample Output #3示例输出 #3
0 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. c1 甚至没有声明。 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 .str1 beginWith 或 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 .代码将str2向右移动一步,并比较str2是否与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.请注意,在str1 = "AAA" 和str2 = "AA" 的情况下,结果为 2,因为第一次和第二次出现的AA都将匹配。
Step 1:第1步:

str1: *****
str2: **

Step 2:第2步:

str1: *****
str2:  **

Step 3:第 3 步:

str1: *****
str2:   **

Step 4:第四步:

str1: *****
str2:    **

If you wish in the above situation, AA only counts once, here is the code for you:如果您希望在上述情况下, AA只计算一次,这里是您的代码:

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:第1步:

str1: *****
str2: **

Step 2:第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.否则,您将需要在 google 上提出一个问题,例如:“计算字符串 java 中的出现次数”之类的。 This will give you - tada - stackoverflow:这会给你 - 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"; //在这里,我们将每个大写字符更改为小写,以便与 s2 进行比较 public int count(String str){ String s1="";String s2="hello"; int l2=s2.length(); int l2=s2.length(); int count=0;整数计数=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;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 返回字符串“ hello”出现在给定字符串中任何位置的次数 - Return the number of times that the string “hello” appears anywhere in the given string 返回字符串“code”在给定字符串中任何位置出现的次数 - Return the number of times that the string "code" appears anywhere in the given string 返回字符串“hi”出现在给定字符串中的任何位置的次数 - Return the number of times that the string “hi” appears anywhere in the given string Java中的字符串可以将“ Hello”转换为Hello - String in Java can we convert “Hello” into Hello 关于java字符串“hello”和“hello”的问题 - Question about java String “hello” and 'hello' 关于String a =“你好”; 字符串b =“hello”a == b,在java中 - about String a= “hello”; String b= “hello” a==b , in java 最终String s =“Hello World”与String s =“Hello World”相同吗? - Is final String s = “Hello World” same as String s = “Hello World”? 当新的String(“ hello”)间接指向“ hello”时,它是否对简单的“ hello”完全没有用? - Does use of new String(“hello”) is completely useless over simple “hello”, when it is indirectly pointing to “hello”? 如何输入字符串内容:encoded =“ Hello”; 在java中? - How to type String content:encoded = “Hello”; in java? 我正在使用 JakartaEE 并制作一个“hello world”类的 restfulAPI,无法通过访问 URL 获得返回字符串 - I'm using JakartaEE and making a "hello world" kind of restfulAPI, can't get a return string by visiting URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM