I have two strings, a <- "AERRRTX"; b <- "TRRA"
.
I want to extract the characters in a
not used in b
, i.e. "ERX"
I tried the answer in Extract characters that differ between two strings , which uses setdiff
. It returns "EX", because b
does have "R" and setdiff
will eliminate all three "R"s in a
. My aim is to treat each character as distinct, so only two of the three R's in a
should be eliminated.
Any suggestions on what I can use instead of setdiff
, or some other approach to achieve my output?
Uncommonstring(s1,s2) /* s1 and s2 are two string */ Step 1: Convert both string into set st1 and st2. Step 2: use the intersection of two sets and get common characters. Step 3: now separate out characters in each string which are not common in both string.
Q: How to get the non-matching characters in a string? Find and print the uncommon characters of the two given strings in sorted order. Here uncommon character means that either the character is present in one string or it is present in other string but not in both.
A unique string consists of characters that occur only once. To check for uniqueness, compare each character with the rest of the string. If a character is repeated, then the string is not unique.
A different approach using pmatch
,
a1 <- unlist(strsplit(a, ""))
b1 <- unlist(strsplit(b, ""))
a1[!1:length(a1) %in% pmatch(b1, a1)]
#[1] "E" "R" "X"
Another example,
a <- "Ronak";b<-"Shah"
a1 <- unlist(strsplit(a, ""))
b1 <- unlist(strsplit(b, ""))
a1[!1:length(a1) %in% pmatch(b1, a1)]
# [1] "R" "o" "n" "k"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With