How to find matching characters in two strings using regex java - java

In a given two words, is it possible to use regex to find multiple strings matching character as well index.
For example:
String1 = cat
String2 = carrot
the first 2 characters and indexes are matching (ca). t does not count because it is not in the same index.
I've tried for loop. However it appears to be not working and not very efficient.
for (int i = 0; i < string1.length(); i++){
for (int j = 0; j < string2.length(); j++){
char ch1 = string1.charAt(i);
char ch2 = string2.charAt(j);
if (ch1 == ch2) {
count char++;
}
}

What you are probably looking for is the longest common prefix.
See Find longest common prefix?
Regex is for pattern matching. It is a solution to a different problem.

For loop can still work for this job to find positions where each string has same char and number of times this occurs:
ArrayList<Integer> places = new ArrayList<Integer>();
for (int i = 0; i < Math.min(string1.length(), string2.length()); i++) {
a = string1.charAt(i);
b = string2.charAt(i);
if (a == b) {
count++;
places.add(i); //To say at which indices the 2 strings have the same chars
}
}

I guess you want to count the number of characters repeated at the same positions in two words. (Not same prefix)
In words cat carrot, you want to get 2 since c and a are in the same position, but t is not.
in words carrot cabra, you will get 3, since c, a and r (4th) are the same in the same position.
You only need to iterate one time over the two strings at the same time:
String string1 = "car";
String string2 = "carrot";
int minLength = Math.min( string1.length(), string2.length() );
int count = 0;
for (int i = 0; i < minLength; i++){
char ch1 = string1.charAt(i);
char ch2 = string2.charAt(i);
if (ch1 == ch2) {
count++;
}
}
We use minLength since we only need to check until the length of the smallest word.
We use string1.charAt(i) and string2.charAt(i), with same index i, since we want to check characters in the same position.

Related

Compare two strings in Java and show the character differences

I'm looking for a way to compare two strings to each other character by character.
It should show whether there are other characters which don't appear in both strings.
Does someone have a solution for?
for(int j=0; j < min; j++) {
s1 = w1.substring(j,j+1);
s2 = w2.substring(j,j+1);
if (!s1.equalsIgnoreCase(s2) ){
counter++;
}
}
This only looks sequential for differences. But I want to find out whether there are differences between those two strings at all.
So **abc** and **cab** should count as a hit
I think what you want is to show that 2 strings have the same letters and the same amount of each letter. Use 2 hashsets, where the key is the character and the value is the number of occurrence in the string. you'll have one hashset for each string, then loop through the string add the characters to the set and compare to see if the sets are equal.
Turn them into charArrays and add the differences to an empty string as a for loop scans through them, for instance
String a = "abc";
char[] aa = a.toCharArray();
String b = "cba";
char[] bb = b.toCharArray();
String dif;
public void differ() {
for(int i = 0; i < aa.length - 1; i++) {
if(!aa[i].equals(bb[i])) {
dif += aa[i];
}
}
}
I believe running differ() would return "ac" because they both have b in the middle. Is this what you wanted?

Delete all matching pairs of a character from a string

I'm trying to make a code that deletes the repeated characters. For example - if we have a string "aabacdc", we want to make it as "abd". If the character exists twice in the string, then we delete both characters as we did in the above example. The 'a' occurs 3 times in our string, so we just deleted the 2 a and left 1 remaining.
What I'm trying to do in this code is use two nested for loops - first for loop to compare the first character with the other characters. If the character has a duplicate in the string, then just delete both the characters. How can I fix this code?
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str2 = input.nextLine();
StringBuilder str = new StringBuilder(str2);
for (int k = 0; k < str.length() - 1; k++) {
for (int i = 1; i < str.length() - 1; i++) {
if (str.charAt(k) == str.charAt(i)) {
str.deleteCharAt(k);
str.deleteCharAt(i);
}
}
}
System.out.println(str);
}
My interpretation of what you're trying to do based on your expected output is that you want to remove characters from the string 1 pair at a time. So if there is an odd number of a character in the string, 1 should remain, and if there's an even number 0 should remain.
Any time you're removing elements from a structure while you're iterating by index, you need to loop over the structure backwards, so that the index values don't shift as you delete elements. This means you should only delete elements which the outer loop is currently at, or has already seen (i.e. only delete elements at indexes >= i).
Scanner input = new Scanner(System.in);
String str = input.nextLine();
StringBuilder sb = new StringBuilder(str);
for (int i = sb.length() - 2; i >= 0; i--) {
for (int j = i + 1; j < sb.length(); j++) {
if (sb.charAt(i) == sb.charAt(j)) {
sb.deleteCharAt(j);
sb.deleteCharAt(i);
break;
}
}
}
System.out.println(sb);
Ideone Demo

calculating the number of string occurances in another string

This is my assignment. I am not allowed to use if statements.
Write a program NumStrings.java that receives two strings through the command line as
input and then prints out the number of times the second string occurs as a substring in the
first.
My bad code:
public class Test {
public static void main(String[] args) {
String a = "HelloHelloHelloHello";
String b = "Hello";
int times = 0;
for(int i=0; i <= a.length()-5; i++){
for (int z=4; z<=(a.length()-1) && a.compareTo(b)==0; z++){
times = times +1;
}
}
System.out.print(times);
}
}
Here is the correct way to do it, using subString() (documentation here: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#substring(int)):
String a = "HelloHelloHelloHello";
String b = "Hello";
int times = 0;
for (int i = 0; i <= a.length() - b.length(); i++) {
String substring = a.subString(i, i + b.length());
if (substring.equals(b)) {
times = times + 1;
}
}
System.out.println(times);
And here is a way to do it without if statements... Which I don't recommend. But if you have to do it that way, this will work.
String a = "HelloHelloHelloHello";
String b = "Hello";
int times = 0;
for (int i = 0; i <= a.length() - b.length(); i++) {
String substring = a.substring(i, i + b.length());
for (int j = 0; substring.equals(b) && j < 1; j++) {
times = times + 1;
}
}
System.out.println(times);
Look at it this way: you don't have to count how often you find the second string in the first String, because you always have to check if you found it or not. So, to avoid all sorts of conditions or if statements, consider using firstString.split(secondString).
split(someString) will return you an array of remaining substrings once you "split" the base string everytime it finds your substring:
String first = "bababa";
String second = "a";
String[] substrings = first.split(second);
now substrings will look like this: ["b", "b", b"] because every a has been removed and the rest put in separate Strings.
Next you have to check the size of the array and you'll see how often your first String was split.
int count = substrings.length; // 3
However, this is not the end of it because we still have the following case:
String first = "bababaa";
With the above solution you would get an array of size 3: ["b", "b", "b"]. The last occurrence of a will only be removed without leaving any substring behind (not even an empty one '').
So you can take advantage of another (slightly different) split():
first.split(second, limit);
Where limit is the maximum number of occurrences the method tries to find. So how often can you find your second string in the first one? As many letters the first string has: int limit = first.length
first.split(second, first.length); // will produce [b, b, b, , ]
Can you see what happens? there are two empty strings at the end where there where two a. You get an array of substrings for everything that is found before or after the occurrence of the second String.
Naturally, when you split the string ba you would get ["b", ] so 2 substrings. But you don't care about the b just the "commas" in the middle (for every a a ,).
first.split(second, first.length).length -1; // that's how many commas there are, and thats how many second strings there are
EDIT
(thanks #saka1029 !) So, the "split" method still misses something when first="aaa" and second="aa" because it counts only 1 not 2 occurrences.
To correct that I thought of looping through the whole first string and checking only for the very first occurrence, and then removing the first letter and continuing (since OP already accepted another answer, I just post my code):
String first = "ZZZ";
String second = "ZZ";
int counter = 0; // counts the number of occurrences
int n = first.length(); // needs to be fixed, because we'll change the length of the first string in the loop
for(int i = 0; i < n; i++){ // check the first string letter by letter
String[] split = first.split(second, 2); // we want one substring and the rest (2 in total) like: ['', 'Z'] when we cut off the first 'ZZ'
counter += split.length - 1; // now add the number of occurrences (which will be either 0 or 1 in our case)
first = first.substring(1); // cut off the first letter of the first string and continue
}
System.out.println("counter = " + counter); // now we should get 3 for 'ZZZ'

Find char in string that is a digit and larger or equal to 2

I have a string that contains numbers like: 02101403101303101303140
how can I iterate the string to check whether the number in string is >= 2 and remember that number's index in array or list for further processing?
the further processing should be replacing substrings.
for example: the iterator found number 2 and remembers the index of this character.
Now it takes the next character from 2 and remembers this character index also.
Now it is possible to replace the substring.
Let's say there is 21. Now I want this to become 11
Or lets say there is 60, this should be replaced with 000000.
First number is indicator of "how many" and the second number is "what".
Or is there a better way to remember and replace certain substrings in that way?
Thank you in advance.
There you go. but remember to atleast try next time
String str = "02101403101303101303140";
StringBuilder sb = new StringBuilder();
for(int i=0; i < str.length(); i+=2)
for(int j =0; j < Integer.parseInt(String.valueOf(str.charAt(i))); j++)
sb.append(str.charAt(i+1));
System.out.print(sb.toString());
Not sure if I'm understanding well your question, you could try something like this:
String mystring = "02101403101303101303140";
String target = "21";
String replacement = "11"
String newString = mystring.replace(target, replacement);
String str = "02101403101303101303140";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if(Integer.parseInt(String.valueOf(str.charAt(i))) >= 2) {
int temp = Integer.parseInt(String.valueOf(str.charAt(i))) - 1;
for (int j = 0; j < temp ; j++) {
sb.append(str.charAt(i+1));
}
}
else {
sb.append(str.charAt(i));
}
}
System.out.println(sb.toString());
This would produce: 01101000011101000111010001110000 which is binary for "http" (without quotes).
Thank you all! What I really needed was a push to right direction and thank zubergu for that. Also fr34k gave the best answer!

Given a string S and a string T, count the number of distinct subsequences of T in S

Hi All: can someone explain to me how this algorithm works? I fail to understand the mechanism. Thanks.
Problem: Given a string S and a string T, count the number of distinct subsequences of T in S.
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ACE" is a subsequence of "ABCDE" while "AEC" is not).
Here is an example:
S = "rabbbit", T = "rabbit"
Return 3.
Solution:
public int numDistincts(String S, String T)
{
int[][] table = new int[S.length() + 1][T.length() + 1];
for (int i = 0; i < S.length(); i++)
table[i][0] = 1;
for (int i = 1; i <= S.length(); i++) {
for (int j = 1; j <= T.length(); j++) {
if (S.charAt(i - 1) == T.charAt(j - 1)) {
table[i][j] += table[i - 1][j] + table[i - 1][j - 1];
} else {
table[i][j] += table[i - 1][j];
}
}
}
return table[S.length()][T.length()];
}
The above DP solution using O(m*n) space, where m is the length of S, and n is the length of T. Below is the Solution that has only O(n) space.
public class Solution {
public int numDistinct(String s, String t) {
if(s == null || t == null || t.length() == 0) return 0;
int[] dp = new int[t.length()];
for(int i = 0; i<s.length(); i++){
char c = s.charAt(i);
for(int j=dp.length-1; j>=0; j--){
if(c == t.charAt(j)){
dp[j] = dp[j] + (j!=0?dp[j-1]: 1);
}
}
}
return dp[t.length()-1];
}
}
From this page.
First of all, note that += can just as well be =, because each combination of [i][j] is visited only once - in fact = would be better because it wouldn't have to use the fact that in Java ints are initialised to 0.
This is a dynamic programming solution. table[i][j] ends up storing the answer when you consider only the first i characters of S and the first i characters of T.
The first loop says that if T is the zero length string the only subsequence of T in S is the zero length subsequence - there is one of these.
The second loop compares the ith character of S with the jth character of T at a time when these are both the last character of the short strings being dealt with. If these don't match the only subsequences of T in S are also sub-sequences of S with the last non-matching character chopped off, and we have already calculated these in table[i-1][j]. If they do match then there are extra subsequences that match this last character. If you take that last character off the subsequence then you find a subsequence from this segment of T with one character lopped off that matches one from S with one character lopped off, and you have already counted them in table[i-1][j-1] - so for a match the answer is table[i-1][j] + table[i-1][j-1].
At the end, of course, you find that you have calculated the answer for the full length of S and T in table[s.length][t.length]

Categories