Java substring appearance times (Huawei machine questions)

Source: Internet
Author: User

For example, the sub-string "AA" of "aaaa" appears three times.

Package COM. handy. DS; import Java. util. stack; import Java. util. arrays; public class teststring {/*** @ Param ARGs */public static void main (string [] ARGs) {// todo auto-generated method stubteststring Ts = new teststring (); system. out. println (TS. compareto1 ("abababab", "Bab"); system. out. println (TS. compareto2 ("abababab", "Bab"); system. out. println (TS. compareto3 ("abababab", "Bab");} // idea: str1 = ababac, S Tr2 = AB // the combination of all str2 lengths (2) of str1, AB, Ba, AB, Ba, AC // compare these combinations with str2 respectively, equal count + 1 Public String compareto1 (string str1, string str2) {string Ss = NULL; int COUNT = 0; For (INT I = 0; I <= str1.length () -str2.length (); I ++) {Ss = str1.substring (I, I + str2.length (); If (ss. equals (str2) Count ++;} return string. valueof (count);} // idea: for example, str1 = ababac, str2 = AB // the combination of all 2 lengths (2) of str1, AB, Ba, AB, BA, AC // compare these combinations with str2 respectively. Count + 1 // similar to the first method, but the public string compareto2 (string str1, string str2) {stack S = new stack (); int COUNT = 0; for (INT I = 0; I <= str1.length ()-str2.length (); I ++) {S. add (str1.substring (I, I + str2.length ();} int I = 0; while (S. size ()! = 0) {If (S. pop (). tostring (). equals (str2) I ++;} return string. valueof (I);} // thought: Find the same position as the first letter of str2 in str1, then take the substring of the same length as str2, and compare it with str2, if the value is equal, Count + 1 // For example, str1 = abcab, str2 = AB. In str1, A is the same as a in str2, and AB is equal to str2, count + 1 // then take BC, Ca, and AB, and there is another AB, so count = 2 Public String compareto3 (string str1, string str2) {int COUNT = 0; int I = 0; string temp = NULL; char C; Boolean iscount = false; If (str1.length () <str2.length () Return "0"; while (str2.length () + I <= str1.length () {c = str1.charat (I); If (C = str2.charat (0) {temp = str1.substring (I, I + str2.length ()); iscount = true;} elseiscount = false; If (iscount & temp. equals (str2) Count ++; I ++;} return string. valueof (count );}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.