Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
 String find = "cdc";
 StringBuffer found = new StringBuffer();
 int count = 0;
 int j=0;
 for ( int i = 0; i <input.length();){
 j = 0;
 while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
 found.append(input.charAt(i));
 j++;
 i++;
 }
 i=i-j;
 if(found.toString().equals(find)) {
 count++;
 }
 i++;
 found.setLength(0);
 }
 System.out.println("count is " + count);
 }

I have another question here here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

Update: For all the people down voting, this is not a duplicate but a variant of the other question. If posting variants of a question is against the rules of stackexchange, please let me know and I'll delete my other post. Otherwise, I'd like to keep the other post because my solutions are slightly different in approach and much different in complexity and I'd like a feedback on both of them. Posting them as part of one post would be messy, both for me, and for the people trying to give feedback.

Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
 String find = "cdc";
 StringBuffer found = new StringBuffer();
 int count = 0;
 int j=0;
 for ( int i = 0; i <input.length();){
 j = 0;
 while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
 found.append(input.charAt(i));
 j++;
 i++;
 }
 i=i-j;
 if(found.toString().equals(find)) {
 count++;
 }
 i++;
 found.setLength(0);
 }
 System.out.println("count is " + count);
 }

I have another question here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

Update: For all the people down voting, this is not a duplicate but a variant of the other question. If posting variants of a question is against the rules of stackexchange, please let me know and I'll delete my other post. Otherwise, I'd like to keep the other post because my solutions are slightly different in approach and much different in complexity and I'd like a feedback on both of them. Posting them as part of one post would be messy, both for me, and for the people trying to give feedback.

Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
 String find = "cdc";
 StringBuffer found = new StringBuffer();
 int count = 0;
 int j=0;
 for ( int i = 0; i <input.length();){
 j = 0;
 while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
 found.append(input.charAt(i));
 j++;
 i++;
 }
 i=i-j;
 if(found.toString().equals(find)) {
 count++;
 }
 i++;
 found.setLength(0);
 }
 System.out.println("count is " + count);
 }

I have another question here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

Update: For all the people down voting, this is not a duplicate but a variant of the other question. If posting variants of a question is against the rules of stackexchange, please let me know and I'll delete my other post. Otherwise, I'd like to keep the other post because my solutions are slightly different in approach and much different in complexity and I'd like a feedback on both of them. Posting them as part of one post would be messy, both for me, and for the people trying to give feedback.

added 506 characters in body
Source Link

Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
 String find = "cdc";
 StringBuffer found = new StringBuffer();
 int count = 0;
 int j=0;
 for ( int i = 0; i <input.length();){
 j = 0;
 while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
 found.append(input.charAt(i));
 j++;
 i++;
 }
 i=i-j;
 if(found.toString().equals(find)) {
 count++;
 }
 i++;
 found.setLength(0);
 }
 System.out.println("count is " + count);
 }

I have another question here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

Update: For all the people down voting, this is not a duplicate but a variant of the other question. If posting variants of a question is against the rules of stackexchange, please let me know and I'll delete my other post. Otherwise, I'd like to keep the other post because my solutions are slightly different in approach and much different in complexity and I'd like a feedback on both of them. Posting them as part of one post would be messy, both for me, and for the people trying to give feedback.

Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
 String find = "cdc";
 StringBuffer found = new StringBuffer();
 int count = 0;
 int j=0;
 for ( int i = 0; i <input.length();){
 j = 0;
 while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
 found.append(input.charAt(i));
 j++;
 i++;
 }
 i=i-j;
 if(found.toString().equals(find)) {
 count++;
 }
 i++;
 found.setLength(0);
 }
 System.out.println("count is " + count);
 }

I have another question here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
 String find = "cdc";
 StringBuffer found = new StringBuffer();
 int count = 0;
 int j=0;
 for ( int i = 0; i <input.length();){
 j = 0;
 while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
 found.append(input.charAt(i));
 j++;
 i++;
 }
 i=i-j;
 if(found.toString().equals(find)) {
 count++;
 }
 i++;
 found.setLength(0);
 }
 System.out.println("count is " + count);
 }

I have another question here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

Update: For all the people down voting, this is not a duplicate but a variant of the other question. If posting variants of a question is against the rules of stackexchange, please let me know and I'll delete my other post. Otherwise, I'd like to keep the other post because my solutions are slightly different in approach and much different in complexity and I'd like a feedback on both of them. Posting them as part of one post would be messy, both for me, and for the people trying to give feedback.

Source Link

Find substring count without string functions

Find the substring count from a string without string functions in Java. Given String str = "abcdefghcde"; and String find = "cde";, count occurrences of "cde" in String str.

Taking into account overlaps.

Example: String str = "cdcdcdc"; and String find = "cdc";, occurrence count is 3

Please give any feedback about improving it or any issues (performance or otherwise) that you see.

String input = "cdcdcdcdcdcddc";
 String find = "cdc";
 StringBuffer found = new StringBuffer();
 int count = 0;
 int j=0;
 for ( int i = 0; i <input.length();){
 j = 0;
 while(j<find.length() && i<input.length() && input.charAt(i) == find.charAt(j)) {
 found.append(input.charAt(i));
 j++;
 i++;
 }
 i=i-j;
 if(found.toString().equals(find)) {
 count++;
 }
 i++;
 found.setLength(0);
 }
 System.out.println("count is " + count);
 }

I have another question here which doesn't take into account overlaps and it's complexity is \$O(n)\$. But for this question, I'm unable to achieve \$O(n)\$ time running time.

lang-java

AltStyle によって変換されたページ (->オリジナル) /