This is the problem on Leetcode: https://leetcode.com/problems/interleaving-string/
I understand that we could use DP to solve this problem, but I have been trying to solve it with the most intuitive method which is recursion. That being said, I have been trying for days and cannot find the mistake in my code. It failed the testcase s1 = "aabcc", s2 = "dbbca", s3 = "aadbbcbcac".
Can anyone please let me know where does it wrong in the code below?
Thank yo very much!
bool isInterleave(string s1, string s2, string s3) {
if (s3.size()<=0){
return true;
}
else if (s1.size()<=0 && s2.size()<=0 || s1.size()+s2.size()<s3.size()){
return false;
}
if (s1[0]==s2[0] && s1[0]==s3[0] && s2[0]==s3[0]){
return (isInterleave(s1.erase(0,1),s2,s3.erase(0,1)) || isInterleave(s1, s2.erase(0,1), s3.erase(0,1)));
}
else if (s1[0]==s3[0]){
return (isInterleave(s1.erase(0,1),s2,s3.erase(0,1)) || isInterleave(s1,s2.erase(0,1),s3));
}
else if (s2[0]==s3[0]){
return (isInterleave(s1, s2.erase(0,1), s3.erase(0,1)) || isInterleave(s1.erase(0,1), s2, s3));
}
if (s1.size()<=0){
return isInterleave(s1, s2.erase(0,1), s3);
}
else{
return isInterleave(s1.erase(0,1), s2, s3);
}
}
Related
I just started learning C# and while loops are confusing me. Unlike Java, where I can use a while loop to loop a program if a user entered a invalid input, it's not acting the same way in C#.
using System;
namespace first {
class Program {
static void Main(string[] args) {
Console.WriteLine("Hi! What is your name");
string userName = Console.ReadLine();
Console.WriteLine("oh! you are:" + userName);
Console.WriteLine("let play a game");
string answer="Y";
while (answer == "Y") {
Random random = new Random();
int correntNumber = random.Next(1, 2);
int guess = 0;
Console.WriteLine("Guess a number");
while (guess != correntNumber) {
string userGuess = Console.ReadLine();
//validate input method 1
try {
guess = int.Parse(userGuess);
} catch (Exception e) {
Console.WriteLine("Invalid inout", e);
}
//validate input method 2
//if(!int.TryParse(userGuess, out guess)) {
// Console.WriteLine("invalid input");
//}
if (guess != correntNumber) {
Console.WriteLine("try again!");
}
}
Console.WriteLine("Yes! corrector");
Console.WriteLine("Play again?");
//string answer;
answer = Console.ReadLine().ToUpper();
if(answer == "Y") {
continue;
} else if (answer == "N") {
Console.WriteLine("bye");
return;
} else if (answer != "Y" || answer != "N") {
Console.WriteLine("y or n");
answer = Console.ReadLine().ToUpper();
continue;
}
}
}
}
}
When I enter a value other than y or n, the message appears,Console.WriteLine("Y or n only");, but the game restarts while it shouldn't.
I am sorry this is a simple and rather silly question, but I can't pin point where I am going wrong.
the problem is that after printing to the user "y or n only" message you take the input but you don't actually do anything with it
so the loop just restarts regardless of the input , to fix this issue you could replace the last if part with this code
while(answer != 'Y' && answer != 'N'){
Console.WriteLine("y or n only");
answer = Convert.ToChar(Console.ReadLine().ToUpper());
}
if(answer == 'Y')
{
continue;
}
else if(answer == 'N')
{
Console.WriteLine("goodbye");
return;
}
so after you read the first input answer of him for repeating or no you check if it's a valid input or not and if it's not you keep asking him for "y or n only" till he enters "Y" or "N" and then you process this answer for whether it's a "Y" or "N" in the if part
I cannot get a function for this code to really work.
lastVal = val;
val = digitalRead(DT);
if (val == 1 && lastVal == 0)
{
if (digitalRead(CLK) == 1)
{
pos++;
}
else
{
pos--;
}
}
Can somebody pleas help me?
I am not sure if this is your entire code or not, but in case that is all of the code then I know the reason. Arduino requires the basic setup and loop functions to be referenced in the code, as long as it is referenced you should be fine - you can even leave the inside of the functions empty. You have not really asked the question very well so it is hard to see what you mean.
To create a function you can use this code:
void function_name_here(_parameters_here_)
{
//Code Here
}
To reference that function you just declare it by using:
function_name_here();
By the looks of it you might want to put your code in to the loop function, your code might look like this:
int DT = /* Value here */;
int pos = /* Value here */;
void setup()
{
pinMode(DT, INPUT);
}
void loop()
{
lastVal = val;
val = digitalRead(DT);
if (val == 1 && lastVal == 0)
{
if (digitalRead(CLK) == 1)
{
pos++;
}
else
{
pos--;
}
}
}
I am trying to find the K-th largest element in a Binary Search Tree using reverse inorder approach by using a counter. Here is what I have implemented:
int klargest(Node root,int k,int count)
{
if(root != null)
{
klargest(root.right,k,count);
count++;
if(count == k)
return root.data;
klargest(root.left,k,count);
}
return -1;
}
But the issue is that when count = k, the code does not return the answer to the caller function but instead to a sub-call. Due to this, the answer is lost. In other words, the recursion does not stop there and it keeps on going until all the nodes are visited. In the end, I get the answer -1. What I want is that the recursion should end when count = k and the required answer should be returned to the caller function. How can I do this?
Note: I neither want to use a global variable nor an iterative approach.
Actually, you do not return your node - the recursive call results are ignored. Rewrite it so it returns a Node:
Node klargest(Node root,int k,int count)
{
Node result = null;
if(root != null)
{
result = klargest(root.right,k,count);
if (result != null)
return result;
count++;
if(count == k)
return root;
result = klargest(root.left,k,count);
}
return result;
}
You can use this approach:
int kthLargest(Node node, int k) {
int rightCount = count(node.right);
if (k <= rightCount) {
return kthLargest(node.right, k);
} else if (k == rightCount+1) {
return node.data;
} else {
return kthLargest(node.left, k - rightCount + 1);
}
}
int count(Node node) {
if (node != null) {
return count(node.left) + count(node.right) + 1;
}
return 0;
}
I am trying to write a simple mutually recursive function in Haxe 3, but couldn't get the code to compile because whichever one of the mutual functions that appears first will report that the other functions in the group is undefined. A minimal example is below, in which mutually defined functions odd and even are used to determine parity.
static public function test(n:Int):Bool {
var a:Int;
if (n >= 0) a = n; else a = -n;
function even(x:Int):Bool {
if (x == 0)
return true;
else
return odd(x - 1);
}
function odd(x:Int):Bool {
if (x == 0)
return false;
else
return even(x - 1);
}
return even(a);
}
Trying to compile it to neko gives:
../test.hx:715: characters 11-14 : Unknown identifier : odd
Uncaught exception - load.c(181) : Module not found : main.n
I tried to give a forward declaration of odd before even as one would do in c/c++, but it seems to be illegal in haxe3. How can one define mutually-recursive functions like above? Is it possible at all?
Note: I wanted to have both odd and even to be local functions wrapped in the globally visible function test.
Thanks,
Rather than using the function myFn() {} syntax for a local variable, you can use the myFn = function() {} syntax. Then you are able to declare the function type signiatures before you use them.
Your code should now look like:
static public function test(n:Int):Bool {
var a:Int;
if (n >= 0) a = n; else a = -n;
var even:Int->Bool = null;
var odd = null; // Leave out the type signiature, still works.
even = function (x:Int):Bool {
if (x == 0)
return true;
else
return odd(x - 1);
}
odd = function (x:Int):Bool {
if (x == 0)
return false;
else
return even(x - 1);
}
return even(a);
}
This works because Haxe just needs to know that even and odd exist, and are set to something (even if it's null) before they are used. We know that we'll set both of them to callable functions before they are actually called.
See on try haxe: http://try.haxe.org/#E79D4
I've been running through codeacademy's tutorials and projects. On FizzBuzz++ 1.5 they want us to re-write the "Wobble" function as Wob, using ternary operators. I keep getting an error saying "missing operand" with the following code. Also the +1 on the end of the return, how does that work, does javaScript store it as a temp value because it doesn't get assigned to any var. Thanks for the help. Code is below.
var Wibble = {
Wobble: function(a, b) {
if(a===b)
//if the variables are equal return 0
return 0;
else {
//decrement the larger of the 2 values
if (a>b) {
a--;
} else {
b--;
}
//call this function again with the decremented values
//once they are equal the functions will return up the stack
//adding 1 to the return value for each recursion
return Wibble.Wobble(a,b)+1;
}
},
//This is the line of code that doesn't want to function..
Wob: function(a, b) {
(a===b) ? return 0 :(a<b) ? return this.Wob(a--,b)+1 : return this.Wob(a,b--)+1;
}
};
The following expression with the ternary operator:
result = (a) ? x : y;
is equivalent to the following:
if(a)
{
result = x;
}
else
{
result = y;
}
Note the syntactical difference, where in the ternary operator you are switching in the assignment, whereas in the if statement syntax, you are assigning in the switch.
That is to say that:
(a == b) ? return 0 : return 1;
is not equivalent to:
if(a == b)
return 0;
else
return 1;
Instead, you would want to write:
return (a == b) ? 0 : 1;