Given two integers A and B return string containing letters “a” and “b” with no three consecutive...
Asked in coding challenge.
Example:
A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa
string algorithm data-structures greedy
add a comment |
Asked in coding challenge.
Example:
A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa
string algorithm data-structures greedy
Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20
1
So, what is your problem?
– Andreas
Nov 13 at 6:28
This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30
1
Comparea
andb
values. Let's say ifa
is larger, then create a string with onlya
's and then insertb
after every 2a
's. If there are stillb
's left, then insertb
afterb
that was attached.
– vivek_23
Nov 13 at 8:04
if there are stillb
's left, attach them at the beginning and end of the string.
– vivek_23
Nov 13 at 8:34
add a comment |
Asked in coding challenge.
Example:
A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa
string algorithm data-structures greedy
Asked in coding challenge.
Example:
A = 5 and B = 3 return "aabaabab" or "abbaabaa" both are correct answers
A = 1 and B = 4 return "bbabb"
constraints : ignore cases where A = 0 and B >= 3 vice-versa
string algorithm data-structures greedy
string algorithm data-structures greedy
asked Nov 13 at 6:18
Shailesh Shekhawat
135113
135113
Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20
1
So, what is your problem?
– Andreas
Nov 13 at 6:28
This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30
1
Comparea
andb
values. Let's say ifa
is larger, then create a string with onlya
's and then insertb
after every 2a
's. If there are stillb
's left, then insertb
afterb
that was attached.
– vivek_23
Nov 13 at 8:04
if there are stillb
's left, attach them at the beginning and end of the string.
– vivek_23
Nov 13 at 8:34
add a comment |
Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20
1
So, what is your problem?
– Andreas
Nov 13 at 6:28
This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30
1
Comparea
andb
values. Let's say ifa
is larger, then create a string with onlya
's and then insertb
after every 2a
's. If there are stillb
's left, then insertb
afterb
that was attached.
– vivek_23
Nov 13 at 8:04
if there are stillb
's left, attach them at the beginning and end of the string.
– vivek_23
Nov 13 at 8:34
Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20
Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20
1
1
So, what is your problem?
– Andreas
Nov 13 at 6:28
So, what is your problem?
– Andreas
Nov 13 at 6:28
This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30
This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30
1
1
Compare
a
and b
values. Let's say if a
is larger, then create a string with only a
's and then insert b
after every 2 a
's. If there are still b
's left, then insert b
after b
that was attached.– vivek_23
Nov 13 at 8:04
Compare
a
and b
values. Let's say if a
is larger, then create a string with only a
's and then insert b
after every 2 a
's. If there are still b
's left, then insert b
after b
that was attached.– vivek_23
Nov 13 at 8:04
if there are still
b
's left, attach them at the beginning and end of the string.– vivek_23
Nov 13 at 8:34
if there are still
b
's left, attach them at the beginning and end of the string.– vivek_23
Nov 13 at 8:34
add a comment |
2 Answers
2
active
oldest
votes
As per above problem my JAVA solution is:
public static String solution(int A, int B) {
String biggerString, smallerString;
int biggerCount, smallerCount;
if (A >= B) {
biggerString = "a";
smallerString = "b";
biggerCount = A;
smallerCount = B;
} else {
biggerString = "b";
smallerString = "a";
biggerCount = B;
smallerCount = A;
}
// A > 2B + 2
if (biggerCount > 2 * smallerCount + 2) {
return "No Soulution";
}
StringBuilder returnString = new StringBuilder();
do {
if (biggerCount == smallerCount + 1) {
returnString.append(biggerString);
biggerCount--;
} else if (biggerCount == smallerCount) {
returnString.append(biggerString);
biggerCount--;
returnString.append(smallerString);
smallerCount--;
} else {
returnString.append(biggerString);
returnString.append(biggerString);
biggerCount -= 2;
if (smallerCount > 0) {
returnString.append(smallerString);
smallerCount -= 1;
}
}
} while (biggerCount > 0);
return returnString.toString();
}
add a comment |
Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).
If A > 2B + 2
, there is no solution, as you don't have enough letters b to fill the gaps between a's.
Keep inserting 2 a
's and 1 b
's until there is equal number of them or there is no more b
's. After that place 1 of each consecutively.
At the end you'll be left with no more letters, or 1 more letter a
. Place it at the end. You're done.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274921%2fgiven-two-integers-a-and-b-return-string-containing-letters-a-and-b-with-no%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
As per above problem my JAVA solution is:
public static String solution(int A, int B) {
String biggerString, smallerString;
int biggerCount, smallerCount;
if (A >= B) {
biggerString = "a";
smallerString = "b";
biggerCount = A;
smallerCount = B;
} else {
biggerString = "b";
smallerString = "a";
biggerCount = B;
smallerCount = A;
}
// A > 2B + 2
if (biggerCount > 2 * smallerCount + 2) {
return "No Soulution";
}
StringBuilder returnString = new StringBuilder();
do {
if (biggerCount == smallerCount + 1) {
returnString.append(biggerString);
biggerCount--;
} else if (biggerCount == smallerCount) {
returnString.append(biggerString);
biggerCount--;
returnString.append(smallerString);
smallerCount--;
} else {
returnString.append(biggerString);
returnString.append(biggerString);
biggerCount -= 2;
if (smallerCount > 0) {
returnString.append(smallerString);
smallerCount -= 1;
}
}
} while (biggerCount > 0);
return returnString.toString();
}
add a comment |
As per above problem my JAVA solution is:
public static String solution(int A, int B) {
String biggerString, smallerString;
int biggerCount, smallerCount;
if (A >= B) {
biggerString = "a";
smallerString = "b";
biggerCount = A;
smallerCount = B;
} else {
biggerString = "b";
smallerString = "a";
biggerCount = B;
smallerCount = A;
}
// A > 2B + 2
if (biggerCount > 2 * smallerCount + 2) {
return "No Soulution";
}
StringBuilder returnString = new StringBuilder();
do {
if (biggerCount == smallerCount + 1) {
returnString.append(biggerString);
biggerCount--;
} else if (biggerCount == smallerCount) {
returnString.append(biggerString);
biggerCount--;
returnString.append(smallerString);
smallerCount--;
} else {
returnString.append(biggerString);
returnString.append(biggerString);
biggerCount -= 2;
if (smallerCount > 0) {
returnString.append(smallerString);
smallerCount -= 1;
}
}
} while (biggerCount > 0);
return returnString.toString();
}
add a comment |
As per above problem my JAVA solution is:
public static String solution(int A, int B) {
String biggerString, smallerString;
int biggerCount, smallerCount;
if (A >= B) {
biggerString = "a";
smallerString = "b";
biggerCount = A;
smallerCount = B;
} else {
biggerString = "b";
smallerString = "a";
biggerCount = B;
smallerCount = A;
}
// A > 2B + 2
if (biggerCount > 2 * smallerCount + 2) {
return "No Soulution";
}
StringBuilder returnString = new StringBuilder();
do {
if (biggerCount == smallerCount + 1) {
returnString.append(biggerString);
biggerCount--;
} else if (biggerCount == smallerCount) {
returnString.append(biggerString);
biggerCount--;
returnString.append(smallerString);
smallerCount--;
} else {
returnString.append(biggerString);
returnString.append(biggerString);
biggerCount -= 2;
if (smallerCount > 0) {
returnString.append(smallerString);
smallerCount -= 1;
}
}
} while (biggerCount > 0);
return returnString.toString();
}
As per above problem my JAVA solution is:
public static String solution(int A, int B) {
String biggerString, smallerString;
int biggerCount, smallerCount;
if (A >= B) {
biggerString = "a";
smallerString = "b";
biggerCount = A;
smallerCount = B;
} else {
biggerString = "b";
smallerString = "a";
biggerCount = B;
smallerCount = A;
}
// A > 2B + 2
if (biggerCount > 2 * smallerCount + 2) {
return "No Soulution";
}
StringBuilder returnString = new StringBuilder();
do {
if (biggerCount == smallerCount + 1) {
returnString.append(biggerString);
biggerCount--;
} else if (biggerCount == smallerCount) {
returnString.append(biggerString);
biggerCount--;
returnString.append(smallerString);
smallerCount--;
} else {
returnString.append(biggerString);
returnString.append(biggerString);
biggerCount -= 2;
if (smallerCount > 0) {
returnString.append(smallerString);
smallerCount -= 1;
}
}
} while (biggerCount > 0);
return returnString.toString();
}
answered Nov 22 at 17:41
Amrith Raj Herle
1268
1268
add a comment |
add a comment |
Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).
If A > 2B + 2
, there is no solution, as you don't have enough letters b to fill the gaps between a's.
Keep inserting 2 a
's and 1 b
's until there is equal number of them or there is no more b
's. After that place 1 of each consecutively.
At the end you'll be left with no more letters, or 1 more letter a
. Place it at the end. You're done.
add a comment |
Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).
If A > 2B + 2
, there is no solution, as you don't have enough letters b to fill the gaps between a's.
Keep inserting 2 a
's and 1 b
's until there is equal number of them or there is no more b
's. After that place 1 of each consecutively.
At the end you'll be left with no more letters, or 1 more letter a
. Place it at the end. You're done.
add a comment |
Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).
If A > 2B + 2
, there is no solution, as you don't have enough letters b to fill the gaps between a's.
Keep inserting 2 a
's and 1 b
's until there is equal number of them or there is no more b
's. After that place 1 of each consecutively.
At the end you'll be left with no more letters, or 1 more letter a
. Place it at the end. You're done.
Assume A is greater than B, otherwise swap them (keep the track of which belongs to which letter).
If A > 2B + 2
, there is no solution, as you don't have enough letters b to fill the gaps between a's.
Keep inserting 2 a
's and 1 b
's until there is equal number of them or there is no more b
's. After that place 1 of each consecutively.
At the end you'll be left with no more letters, or 1 more letter a
. Place it at the end. You're done.
answered Nov 13 at 9:49
hazeiio
1796
1796
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274921%2fgiven-two-integers-a-and-b-return-string-containing-letters-a-and-b-with-no%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Seems there is something missing in the question, or is this it?
– Surt
Nov 13 at 6:20
1
So, what is your problem?
– Andreas
Nov 13 at 6:28
This is it. We need to write a function that will return string like above
– Shailesh Shekhawat
Nov 13 at 6:30
1
Compare
a
andb
values. Let's say ifa
is larger, then create a string with onlya
's and then insertb
after every 2a
's. If there are stillb
's left, then insertb
afterb
that was attached.– vivek_23
Nov 13 at 8:04
if there are still
b
's left, attach them at the beginning and end of the string.– vivek_23
Nov 13 at 8:34