How to Find String B Missing Characters Based on String a and Add Them to String B

How to find string B missing characters based on string A and add them to string B?

you can use stringA.components(separatedBy

 var stringA = "LetDoItNow"
var stringB = "LetDo"


if stringA.count > stringB.count {
let component = stringA.components(separatedBy: stringB)
if component.count > 1{
stringB = stringB+component[1]
}
}

print(stringB)

You can Configure it better than this to make generic solution

How to find missing character in the second string when we compare two strings? - Coding Question

  1. Let assume that we have two strings a and b.
  2. (optional) Compare lengths to find longer one.
  3. Iterate over them char by char and compare letters at same index.
  4. If both letters are the same, ignore it. If different, add letter from longer string to result and increment index of the longer string by 1.
  5. What's left in longer string is your result.

Pseudocode:

const a = "aabbccc"
const b = "aabcc"

let res = ""

for (let i = 0, j = 0; i <= a.length; i++, j++) {
if (a[i] !== b[j]) {
res += a[i]
i++
}
}

console.log(res)

Find missing character from string based on array element

You can do something like:

vital = ['gmail','@','com','.']
text = "Dan@gmail"

for e in vital:
if e not in text:
print('Missing Character: ',e)

This will output:

Missing Character:  com
Missing Character: .

in this case

How this works:
Basically the for loop is just a foreach element in vital. If this element isn't in the string, print that it's missing

Please help me find what case am I missing

There is no need to insert the letter 't' to determine that two strings can coincide.

It is enough to write the if statement

#include <iterator>
#include <algorithm>

//...

auto win = []( const auto &s1, const auto &s2 )
{
return ( std::size( s1 ) == std::size( s2 ) ) &&
( std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
std::next( std::begin( s2 ) ) ) ||
std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
std::next( std::rbegin( s2 ) ) ) );
};

if ( win( s1, s2 ) )
{
std::cout << "Yes\n";
}
else
{
std::cout << "No\n";
}

That is at first you need to compare that two strings have the same length

std::size( s1 ) == std::size( s2 )

The above record can be also written like

s1.sisze() == s2.size()

Then you need to compare the two strings starting from their second characters using the standard algorithm std::equal This exapression

std::next( std::begin( s1 ) )

positions the iterator to the second character of the string.

And if this condition will return false you should compare the first string starting from its second character with the second string in the reverse order. This expression

std::next( std::rbegin( s2 ) )

position the reverse iterator of the second string to the second character of the string starting from its end.

This compound condition is written as a lambda expression that is called in the if statement.

Here is a demonstration program.

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main()
{
auto win = []( const auto &s1, const auto &s2 )
{
return ( std::size( s1 ) == std::size( s2 ) ) &&
( std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
std::next( std::begin( s2 ) ) ) ||
std::equal( std::next( std::begin( s1 ) ), std::end( s1 ),
std::next( std::rbegin( s2 ) ) ) );
};

std::string s1( "abcd" ), s2( "ebcd" );

if ( win( s1, s2 ) )
{
std::cout << "Yes\n";
}
else
{
std::cout << "No\n";
}

s1 = "man"; s2 = "nam";

if ( win( s1, s2 ) )
{
std::cout << "Yes\n";
}
else
{
std::cout << "No\n";
}

s1 = "abd"; s2 = "ebf";

if ( win( s1, s2 ) )
{
std::cout << "Yes\n";
}
else
{
std::cout << "No\n";
}
}

The program output is

Yes
Yes
No

Trying to match string A if string B is found anywhere before it

Using 2 positive lookarounds, you can assert what is on the left is an opening square bracket (?<=\[)

Then match any char except ] using a negated character class ![^[\]]+ preceded by an exclamation mark and assert what is on the right is a closing square bracket using (?=])

Note that in Javascript the lookbehind is not yet widely supported.

(?<=\[)![^[\]]+(?=])

In the replacement use the matched substring $&

Regex demo

[  "[!foo]",  "[!bar]"].forEach(s =>  console.log(s.replace(/(?<=\[)![^[\]]+(?=])/g, " $& ")))

Check if the letters of one string are in another string

Converting strings to sets of individual symbols remove duplicate symbols, so we can simply compare them:

if set(str1) == set(str2):
print("Both have the same letters!")
else:
print("Nope there are some letters missing..")

Note:

As the order of elements in sets is not important, we may even compare them, e. g.

if set(str1) <= set(str2):        # <= means "is subset" in this context
print("All symbols in str1 are in str2, too.")

or

if set(str1) < set(str2):        # < means "is a proper subset" in this context
print("All symbols in str1 are in str2, too, "
"but str2 has at least 1 symbol not contained in str1.")

Check if multiple strings exist in another string

You can use any:

a_string = "A string is more than its parts!"
matches = ["more", "wholesome", "milk"]

if any(x in a_string for x in matches):

Similarly to check if all the strings from the list are found, use all instead of any.

Find missing letter in string Javascript

There are several problems: you're indexing is mixed up (i.e. off-by-one); your return of undefined should be outside the loop, not in it; you're using str.length in places you shouldn't; you're putting the iteration variable into brackets when you shouldn't:

function fearNotLetter(str) {
var difference;
var missingCharCode;

// i starts at 1, increments to str.length
for (var i = 1; i < str.length; i++) {

// Char code of last letter minus char code of second last letter,
// Char code of second last letter minus char code of third last letter, etc.
// Repeat on a loop and set result equal to test each time.
difference = str.charCodeAt(i) - str.charCodeAt(i - 1);

// If charCode A - charCode B == 1, then letters are in order
// alphabetically and test returns 1.

// If charCode A - charCode B > 1, then letters missing from string.

// So if difference between char codes is more than 1,
// return missing char code and convert to string.
if (difference > 1) {
missingCharCode = str.charCodeAt(i) - 1;
return String.fromCharCode(missingCharCode);
} // End of if.
} // End of loop.

return undefined;
} // End of function.


Related Topics



Leave a reply



Submit