Problem:
Return the number of times that the string "code" appears anywhere in the given string, except we'll accept any letter for the 'd', so "cope" and "cooe" count.
countCode("aaacodebbb") → 1
countCode("codexxcode") → 2
countCode("cozexxcope") → 2
Solution:
public int countCode(String str) { int len = str.length(); int count = 0; String co = "co"; String e = "e"; if (len < 4) return 0; for (int i = 0; i < len - 3; i++) { if (co.compareTo(str.substring(i,i+2)) == 0 && e.compareTo(str.substring(i+3, i+4)) == 0) count++; } return count; }
public int countCode(String str)
ReplyDelete{
int sum = 0;
for (int i = 0; i < str.length()-3; ++i)
{
if (str.substring(i,i+2).equals("co") && str.charAt(i+3) == 'e') sum++;
}
return sum;
}
Or using recursion...
ReplyDeletepublic int countCode(String str) {
if (str.length() < 4)
return 0;
else if (str.charAt(0)=='c' && str.charAt(1)=='o' && str.charAt(3)=='e')
return 1 + countCode(str.substring(4));
else
return countCode(str.substring(1));
}
public int countCode(String str) {
ReplyDeleteint c=0;
for (int i=0; i < str.length()-3; i++)
{
if((str.substring(i,i+4).equals("code"))||str.substring(i,i+4).equals("cole")||str.substring(i,i+4).equals("cope")||str.substring(i,i+4).equals("coze")||str.substring(i,i+4).equals("core"))
{
c = ++c;
}
}
return c;
}
public int countCode(String str) {
ReplyDeleteint result = 0;
for (int i = 0; i <= str.length()-4 ; i++) {
if (str.substring(i,i+4).matches("co[a-z]e")) result++;
}
return result;
}
to take this a step more you can use \\w instead of [a-z]
Deletepublic int countCode(String str) {
ReplyDeleteint count = 0;
for (int i = 0; i < str.length()-3; i++){
if (str.substring(i).startsWith("co") && str.charAt(i+3) == 'e')
count++;
}
return count;
}
Recursion :
ReplyDeletepublic int countCode(String str) {
if(str.length()<=3)
return 0;
if(str.charAt(0)=='c' && str.charAt(1)=='o' && str.charAt(3)=='e' )
return 1+countCode(str.substring(4));
return 0 + countCode(str.substring(1));
}
public int countCode(String str) {
Deleteif(str.length()<=3)
return 0;
if(str.charAt(0)=='c' && str.charAt(1)=='o' && str.charAt(3)=='e' )
return 1+countCode(str.substring(4));
return 0 + countCode(str.substring(1));
}
public int countCode(String str) {
ReplyDeleteint countCode = 0;
for (int i = 0; i < str.length() - 3; i++)
{
if (str.substring(i, i + 2).equals("co") && str.substring(i + 3, i + 4).equals("e"))
{
countCode++;
}
}
return countCode;
}
ReplyDeleteint count = 0;
for (int i=0; i<str.length()-3; i++) {
if (str.substring(i, i+2).equals("co") && str.charAt(i+3) == 'e') {
count++;
}
}
return count;
public int countCode(String str) {
ReplyDeleteint count = 0;
for (int i = 0; i < str.length() - 3; i++) {
if (str.substring(i,i+2).equals("co") && str.charAt(i+3) == 'e') {
count++;
i += 3;
}
}
return count;
}
public int countCode(String str) {
ReplyDeleteint count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.substring(i).startsWith("co") && str.substring(i+3).startsWith("e")) {
count++;
}
}
return count;
}
private final int countCode(String word) {
ReplyDeleteif(word.length() <= 3){
return 0;
}
if(word.charAt(0) == 'c'
&& word.charAt(1) == 'o'
&& word.charAt(3) == 'e'){
return 1 + countCode(word.substring(4));
}
return countCode(word.substring(1));
}
public int countCode(String str) {
ReplyDeleteif(str.length() <= 3){
return 0;
}
if(str.charAt(0) == 'c'
&& str.charAt(1) == 'o'
&& str.charAt(3) == 'e'){
return 1 + countCode(str.substring(4));
}
return countCode(str.substring(1));
}
public int countCode(String str) {
ReplyDeleteif(str.length() <= 3){
return 0;
}
return (str.charAt(0) == 'c'
&& str.charAt(1) == 'o'
&& str.charAt(3) == 'e')? (1 + countCode(str.substring(4))):countCode(str.substring(1));
}
public int countCode(String str) {
ReplyDeleteif(str.length() <= 3){
return 0;
}
return (str.charAt(0) == 'c'
&& str.charAt(1) == 'o'
&& str.charAt(3) == 'e')?
(1 + countCode(str.substring(4))):
countCode(str.substring(1));
}
public int countCode(String str) {
ReplyDeletereturn (str.length() <= 3)? 0:
(str.charAt(0) == 'c' && str.charAt(1) == 'o' && str.charAt(3) == 'e')?
1 + countCode(str.substring(4)) : countCode(str.substring(1));
}
}
public int countCode(String str) {
ReplyDeletereturn (str.length() <= 3)? 0:
(str.charAt(0) == 'c' && str.charAt(1) == 'o' && str.charAt(3) == 'e')?
1 + countCode(str.substring(4)) : countCode(str.substring(1));
}
public int countCode(String str) {
ReplyDeleteint count=0;
for(int i=0;i<str.length()-3;i++){
if((str.substring(i,i+4).equals("code"))||((str.substring(i,i+2).equals("co"))&&str.substring(i+3,i+4).equals("e")))
count++;
}
return count;
}
public int countCode(String str) {
ReplyDeleteint count=0;
for(int i=0;i<str.length()-3;i++){
if(str.substring(i,i+4).matches("co[a-z]e"))
count++;
}
return count;
}
public int countCode(String str) {
ReplyDeleteint count=0;
for(int i=0;i<str.length()-3;i++){
if((str.substring(i,i+2).equals("co"))&&(str.substring(i+3,i+4).equals("e"))){
count++;
}
}return count;
}
public int countCode(String str)
ReplyDelete{
int count=0;
if(str.length()<2)
{
return count ;
}
else
{
for(int i=0;i<=str.length()-4;i++)
{
if(str.charAt(i)=='c' && str.charAt(i+1)=='o' && str.charAt(i+3)=='e')
{
count++;
}
else
{
count=count;
}
}
}
return count;
}
int cont = 0;
ReplyDeletefor (int i=0; i<str.length()-3;i++){
if (str.substring(i,i+2).equals("co") && (str.substring(i+3,i+4).equals("e"))){
cont ++;
}
}
return cont;
Another way
ReplyDeletepublic int countCode(String str) {
return str.split("co\\we", - 1).length - 1;
}
public int countCode(String str) {
ReplyDeleteint code=0;
for(int i=0; i<str.length()-3;i++){
if(str.charAt(i)=='c'&&str.charAt(i+1)=='o'&&str.charAt(i+3)=='e')
code++;
}return code;
}