Get the difference between two strings without using arrays

get difference between two strings python
difference between two strings c++
find difference between two strings c++
find difference between two strings javascript
diff between two strings - leetcode
how to compare two strings in java using if
compare two strings and return the difference python
find difference between two strings c#

I have two strings $A and $B sorted by integers. The implemented function must output {A - B} and {B - A} and the code must be WITHOUT ARRAYS. Any ideas?


$A = "1 2 3 8 9"; 
$B = "2 5 9 10 12 14";


{A - B} = "1 3 8"; 
{B - A} = "5 10 12 14";
$A = "1 2 3 8 9"; 
$B = "2 5 9 10 12 14";

//Build regular expression pattern from $A
$pattern = "/\b".str_replace(" ", "\b|\b", $A)."\b/";

//Remove matched numbers within word boundaries
$result = preg_replace($pattern, "", $B);

//Remove any unwanted whitespace
$result = trim(preg_replace("!\s+!", " ", $result));

echo "{B - A} = " . $result;

Compare two Strings in Java, In Java, objects of String are immutable which means they are Using user- defined function : Define a function to compare values with lexicographically without using Get hold of all the important DSA concepts with the DSA Self Paced Compare Dates in Java � How to compare two arrays in Java? The main difference between Array and String is that Array is a data structure that stores a set of elements of the same data type while String is a set of characters. In brief, String is an array but it only stores characters. On the other hand, an array can store a set of integers, set of double, set of characters, etc.

You can use substring in a loop (and use the ASCII code) for each char to determine if they are equal or not and write them to an other string if they are not the same. If your digit is larger than 1 char you have to make the substring looking for the next space.

Compare two strings lexicographically in Java, The method compareTo() is used for comparing two strings lexicographically in Java. How to compare two strings without using library function? Get hold of all the important DSA concepts with the DSA Self Paced Course at Sort an array of strings lexicographically based on prefix � Number of ways to� The most common difference between array and string is, the array can have any data type while strings are ASCII characters. These characters in strings are terminated with a null character ‘\0’. Strings and arrays work differently in C \ C++ and in Java. In this article, we see a detailed comparison between strings and arrays.

Here's a looping version using strspn and substr to extract numbers from the strings and substr_replace to remove them when necessary:

$a = "1 2 3 8 9"; 
$b = "2 5 9 10 12 14";

$a_offset = 0;
$b_offset = 0;
while ($a_offset < strlen($a) && $b_offset < strlen($b)) {
    $a_length = strspn($a, '0123456789', $a_offset);
    $a_num = substr($a, $a_offset, $a_length);
    $b_length = strspn($b, '0123456789', $b_offset);
    $b_num = substr($b, $b_offset, $b_length);
    if ($a_num < $b_num) {
        // keep the a value
        $a_offset = $a_offset + $a_length + 1;
    elseif ($b_num < $a_num) {
        // keep the b value
        $b_offset = $b_offset + $b_length + 1;
    else {
        // values the same, remove them both
        $a = substr_replace($a, '', $a_offset, $a_length + 1);
        $b = substr_replace($b, '', $b_offset, $b_length + 1);
echo "a - b = $a\nb - a = $b";


a - b = 1 3 8 
b - a = 5 10 12 14

Demo on

Finding the Difference Between Two Strings in Java, tutorial will show how to find the difference between two strings using Java. For this tutorial, we're going to use two existing Java libraries and� Unlike C/C++ Character arrays and Strings are two different things in Java. Both Character Arrays and Strings are a collection of characters but are different in terms of properties. Differences between Strings and Character Arrays:

Java, Java code to compare two strings without using library method, here we will compare character by character of a string in Java program. In this post, we will discuss the difference between string and character array in Java. Unlike C/C++, string and character arrays are two different things in Java. Although, String is itself backed by a final character array underneath, there are few important differences between the two which are covered in this post. 1.

C program to Compare Two Strings without using strcmp(), This program allows the user to enter two string values or two-character array. Next, this compare strings program will use For Loop to iterate every character� Key Differences Between Character Array and String. A character array is a collection of variables which are of character datatype. String is a class that is instantiated to declare strings. Using index value you can access a character from a character array.

String Length, String Comparison, strcmp is used to compare two different C strings. When the strings passed to strcmp contains exactly same characters in every index and have exactly same� Another common source of confusion is the difference between using length to get the size of an array and length() to get the size of a String. Notice the presence of parentheses when working with Strings. (array.length is a variable, while String.length() is a method specific to the String class.) Syntax: datatype[] var var[element] = value

  • You have to show us some of your code effort, so that we can correct that. Otherwise it seems that you asked us to do code for you.
  • You can use explode. array_diff(explode(" ", $A), eplode(" ", $B));
  • @TrueTiem: OP not want to use array
  • @devpro actually it's not array, still string. Only converting to array while using array_diff
  • code must be WITHOUT ARRAYS @TrueTiem
  • @eoitos I'm glad I could help! Please don't forget to accept my answer by clicking the checkmark to the left of it, if it solved your problem.
  • I thought that I could use the explode function
  • @eoitos the return value of explode is of what type …?
  • There are many different ways as mentioned in the comments and I also recommend use explode, but this function transforms a string into an array by your selected char. Your question was for an answere without arrays.
  • Exactly! Without array... Is no easy, I spend 3 days and i've not the solution