Problem:
Given two strings, word and a separator, return a big string made of count occurences of the word, separated by the separator string.
repeatSeparator("Word", "X", 3) → "WordXWordXWord"
repeatSeparator("This", "And", 2) → "ThisAndThis"
repeatSeparator("This", "And", 1) → "This"
Solution:
public String repeatSeparator(String word, String sep, int count) { String newWord = ""; for (int i = 0; i < count; i++) { if (i < count-1) newWord += word + sep; else newWord += word; } return newWord; }
{
ReplyDeleteString mix = "";
if(word.length() == 0 || count == 0) return "";
for(int i=0; i < count; i++){
mix = mix + word + sep;
}
if(count >= 1){
mix = mix.substring(0 , mix.length() - sep.length() );
}
return mix;
}
String result = word;
ReplyDeleteif(count==0)
return "";
for(int i=0; i<count-1; i++)
{
result+=sep + word;
}
return result;
Deletepublic String repeatSeparator(String word, String sep, int count) {
String temp="";
for(int a=0;a<count;a+=1)
{
temp=temp+word+sep;
}
return temp.substring(0,temp.length()-sep.length());
}
String s="";
ReplyDeleteif(count>0){
for(int i=0;i<count-1;i++)
{
s = s + word + sep;
}
return s+word;}
return s;
public String repeatSeparator1(String word, String sep, int count) {
ReplyDeleteString str1="";
for(int i = count; i > 0; i--){
str1 += word + sep;
}
return str1.substring(0,str1.length()-sep.length());
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString res = "";
if( count == 0) return res;
res = word;
for(int i = 1; i < count; i++){
res += sep + word;
}
return res;
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString res = "";
if( count == 0) return res;
res = word;
for(int i = 1; i < count; i++){
res += sep + word;
}
return res;
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString result="";
if(count==1){
return word;
}
if(count==0){
return result;
}
for(int i=0;i<count;i++){
if(i==count-1){
result+=word;
}
else{
result+=word+sep;
}
}
return result;
}
This is my solution.
ReplyDeletepublic String repeatSeparator(String word, String sep, int count) {
String c="";
if(count==0) return "";
for(int i=1;i<=count-1;i++){
c+=word+sep;
}
return c+word;
}
Look.
ReplyDeleteIf count is for example 3 word will be 3 times and sep count-1.So i did this :
public String repeatSeparator(String word, String sep, int count) {
String c="";
for(int i=0;i<count;i++){
c+=word+sep;
}
return c.substring(0,c.length()-sep.length());
}
String result = word;
ReplyDeleteif (count==0)
return "";
for (int i=1 ; i< count ; i++)
result =result+sep+word;
return result;
String renwe = "";
ReplyDeletefor(int i =0;i<count;i++) {
renwe += word + sep;
if(i ==count-1) {
renwe = renwe.substring(0,renwe.length()-sep.length());
}
}
return renwe;
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString result = "";
for ( int i = 0; i < count ; i++){
result = result + word + sep;
}
result = result.substring(0,result.length() - sep.length());
return result;
}
ReplyDeleteString temp="";
int a=0;
OUTER: while(a!=count){
temp+=word;
a++;
INNER: while(a!=count){
temp+=sep;
continue OUTER;
}
}
return temp;
Imagine using loops when your solution could be this simple:
ReplyDeletepublic String repeatSeparator(String word, String sep, int count) {
if(count==0) return "";
if(count==2) return (word+sep+word);
if(count==3) return (word+sep+word+sep+word);
if(count==4) return (word+sep+word+sep+word+sep+word);
if(count==5) return (word+sep+word+sep+word+sep+word+sep+word);
if(count==7) return (word+sep+word+sep+word+sep+word+sep+word+sep+word+sep+word);
return word;
}
lmao gene ious
Deleteint a=count;String str1="";
ReplyDeletefor(int j=0;j1)
{
str1+=sep;
count--;
}
}
return str1;
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString newWord = "";
if(count == 1){
return word;
}
for(int i = 0; i < count; i++){
newWord = newWord + word + sep;
}
int s = newWord.lastIndexOf(sep);
return newWord.substring(0,s);
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString newWord="";
for(int i=0;i<count;i++){
if(i==0){
newWord=word;
}
else{
newWord +=sep+word;
}
}
return newWord;
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString newWord="";
if(count>0){
for (int i = 0; i<count-1; i++) {
newWord += word + sep;
}
}else{
return "";
}
return newWord+word;
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString rep = "";
for (int i = 0; i < count; i++)
{
rep += (word + sep);
}
return rep.substring(0, rep.length() - sep.length());
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteif (count == 0 || word.isEmpty()){
return "";
}
if(count == 1){
return word;
}
return word + sep + repeatSeparator(word, sep, count - 1);
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteif (count == 0 || word.isEmpty()){
return "";
}
return (count == 1)?
word:
word + sep + repeatSeparator(word, sep, count - 1);
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeletereturn (count == 0 || word.isEmpty())? "" : (count == 1)?
word : word + sep + repeatSeparator(word, sep, count - 1);
}
String newStr="";
ReplyDeletefor(int i=0;i<count;i++){
newStr +=word+sep;
}
int len = (word.length()*count)+(sep.length()*(count-1));
String s=newStr.substring(0,len);
return s;
public String repeatSeparator(String word, String sep, int count)
ReplyDelete{
String s = "";
for(int i = 0; i < count; i++)
{
if(i < count) s += word;
if(i < count-1) s += sep;
}
return s;
}
public String repeatSeparator(String word, String sep, int count) {
ReplyDeleteString s ="";
if (count == 0)
return s;
for(int i = 0; i < count-1; i++){
s += word;
s += sep;
}
return s + word;
}
String s=((count>0)?(word):(""));
ReplyDeletefor(int i=0; i<count-1; i++)s+=sep+word;
return s;
Another way
ReplyDeletepublic String repeatSeparator(String word, String sep, int count) {
return String.join(sep, Collections.nCopies(count, word));
}