o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)
// Cpp program to check Interleaving of two // strings with no common characters…
- User
// Cpp program to check Interleaving of two // strings with no common characters #include <bits/stdc++.h> using namespace std;
// Returns true if s3 is an interleaving of s1 and s2 bool isIL(string& s1, string& s2, string& s3) {
int i = 0, j = 0; if (s1.size() + s2.size() != s3.size()) return false; // Iterate through all characters of s3 for (int k = 0; k < s3.size(); k++) { // Match the current character of s3 with the // current character of s1. If they match, // move s1 to the next character. if (i < s1.size() && s1[i] == s3[k]) { i++; } // Else match the current character of s3 // with the current character of s2. // If they match, move s2 to the next character. else if (j < s2.size() && s2[j] == s3[k]) { j++; } // If it doesn't match with either s1 or s2, // return false else { return false; } } // If all characters of s3 match with either // s1 or s2. return true;}
int main() { string s1 = "AB"; string s2 = "CD"; string s3 = "ACBD";
if (isIL(s1, s2, s3)) { cout << s3 << " is interleaved of " << s1 << " and " << s2; } else { cout << s3 << " is not interleaved of " << s1 << " and " << s2; } return 0;}
Give me code in Python, Java, C#, JavaScript and C
Remember like Cpp is for C++ and at start replace Cpp with language name
without changing variable name and also do not change comments
have one line space after code when comment is written.
don’t use class if needed then name it GfG
don’t use public if not needed.
and In C program don’t use #define and malloc and calloc instead use simple array
- Assistant
Python
# Python program to check Interleaving of two # strings with no common characters def isIL(s1, s2, s3): # Returns true if s3 is an interleaving of s1 and s2 i = 0 j = 0 if len(s1) + len(s2) != len(s3)): return False # Iterate through all characters of s3 for k in range(len(s3)): # Match the current character of s3 with the # current character of s1. If they match, # move s1 to the next character. if i < len(s1) and s1[i] == s3[k]: i += 1 # Else match the current character of s3 # with the current character of s2. # If they match, move s2 to the next character. elif j < len(s2) and s2[j] == s3[k]: j += 1 # If it doesn't match with either s1 or s2, # return false else: return False # If all characters of s3 match with either # s1 or s2. return True def main(): s1 = "AB" s2 = "CD" s3 = "ACBD" if isIL(s1, s2, s3): print(f"{s3} is interleaved of {s1} and {s2}") else: print(f"{s3} is not interleaved of {s1} and {s2}") if __name__ == "__main__": main()
Java
// Java program to check Interleaving of two // strings with no common characters class GfG { // Returns true if s3 is an interleaving of s1 and s2 static boolean isIL(String s1, String s2, String s3) { int i = 0, j = 0; if (s1.length() + s2.length() != s3.length()) return false; // Iterate through all characters of s3 for (int k = 0; k < s3.length(); k++) { // Match the current character of s3 with the // current character of s1. If they match, // move s1 to the next character. if (i < s1.length() && s1.charAt(i) == s3.charAt(k)) { i++; } // Else match the current character of s3 // with the current character of s2. // If they match, move s2 to the next character. else if (j < s2.length() && s2.charAt(j) == s3.charAt(k)) { j++; } // If it doesn't match with either s1 or s2, // return false else { return false; } } // If all characters of s3 match with either // s1 or s2. return true; } public static void main(String[] args) { String s1 = "AB"; String s2 = "CD"; String s3 = "ACBD"; if (isIL(s1, s2, s3)) { System.out.println(s3 + " is interleaved of " + s1 + " and " + s2); } else { System.out.println(s3 + " is not interleaved of " + s1 + " and " + s2); } } }
C#
// C# program to check Interleaving of two // strings with no common characters using System; class GfG { // Returns true if s3 is an interleaving of s1 and s2 static bool isIL(string s1, string s2, string s3) { int i = 0, j = 0; if (s1.Length + s2.Length != s3.Length) return false; // Iterate through all characters of s3 for (int k = 0; k < s3.Length; k++) { // Match the current character of s3 with the // current character of s1. If they match, // move s1 to the next character. if (i < s1.Length && s1[i] == s3[k]) { i++; } // Else match the current character of s3 // with the current character of s2. // If they match, move s2 to the next character. else if (j < s2.Length && s2[j] == s3[k]) { j++; } // If it doesn't match with either s1 or s2, // return false else { return false; } } // If all characters of s3 match with either // s1 or s2. return true; } static void Main(string[] args) { string s1 = "AB"; string s2 = "CD"; string s3 = "ACBD"; if (isIL(s1, s2, s3)) { Console.WriteLine($"{s3} is interleaved of {s1} and {s2}"); } else { Console.WriteLine($"{s3} is not interleaved of {s1} and {s2}"); } } }
JavaScript
// JavaScript program to check Interleaving of two // strings with no common characters // Returns true if s3 is an interleaving of s1 and s2 function isIL(s1, s2, s3) { let i = 0, j = 0; if (s1.length + s2.length !== s3.length) return false; // Iterate through all characters of s3 for (let k = 0; k < s3.length; k++) { // Match the current character of s3 with the // current character of s1. If they match, // move s1 to the next character. if (i < s1.length && s1[i] === s3[k]) { i++; } // Else match the current character of s3 // with the current character of s2. // If they match, move s2 to the next character. else if (j < s2.length && s2[j] === s3[k]) { j++; } // If it doesn't match with either s1 or s2, // return false else { return false; } } // If all characters of s3 match with either // s1 or s2. return true; } function main() { let s1 = "AB"; let s2 = "CD"; let s3 = "ACBD"; if (isIL(s1, s2, s3)) { console.log(`${s3} is interleaved of ${s1} and ${s2}`); } else { console.log(`${s3} is not interleaved of ${s1} and ${s2}`); } } main();
C
// C program to check Interleaving of two // strings with no common characters #include <stdio.h> #include <string.h> #include <stdbool.h> // Returns true if s3 is an interleaving of s1 and s2 bool isIL(char s1[], char s2[], char s3[]) { int i = 0, j = 0; if (strlen(s1) + strlen(s2) != strlen(s3)) return false; // Iterate through all characters of s3 for (int k = 0; k < strlen(s3); k++) { // Match the current character of s3 with the // current character of s1. If they match, // move s1 to the next character. if (i < strlen(s1) && s1[i] == s3[k]) { i++; } // Else match the current character of s3 // with the current character of s2. // If they match, move s2 to the next character. else if (j < strlen(s2) && s2[j] == s3[k]) { j++; } // If it doesn't match with either s1 or s2, // return false else { return false; } } // If all characters of s3 match with either // s1 or s2. return true; } int main() { char s1[] = "AB"; char s2[] = "CD"; char s3[] = "ACBD"; if (isIL(s1, s2, s3)) { printf("%s is interleaved of %s and %s", s3, s1, s2); } else { printf("%s is not interleaved of %s and %s", s3, s1, s2); } return 0; }