[Solved] Given two strings S and T check how many minimum characters in S we should change such that T occurs as a substring in S

Given two strings S and T check how many minimum characters in S we should change such that T occurs as a substring in S.

Input Format

The first line contains a string S.

The next line contains a string T.

Constraints

1<=|S|,|T|<=1000

The length of T is at most that of S.

S and T consist of lowercase English letters.

Output Format

Print the minimum number of characters in S that need to be changed.

Solution

  1. Take input of string s and t.
  2. Initialize count to 0.
  3. Iterate over the string s from 0 to length of s – length of t + 1.
  4. Initialize c to 0.
  5. Iterate over the string t from i to i + length of t.
  6. If the character at index j of string s is not equal to the character at index j – i of string t, increment c.
  7. If c is less than count or i is 0, assign count to c.
  8. Print count.
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        String t = in.nextLine();
        int count = 0;
        for (int i = 0; i < s.length() - t.length() + 1; i++) {
            int c = 0;
            for (int j = i; j < i + t.length(); j++) {
                if (s.charAt(j) != t.charAt(j - i)) {
                    c++;
                }
            }
            if (c < count || i == 0) {
                count = c;
            }
        }
        System.out.println(count);
    }
}
s = input()
t = input()
count = 0
for i in range(len(s) - len(t) + 1):
    c = 0
    for j in range(i, i + len(t)):
        if s[j] != t[j - i]:
            c += 1
    if c < count or i == 0:
        count = c
print(count)
#include <iostream>
#include <string>

using namespace std;

int main() {
    string s, t;
    cin >> s >> t;
    int count = 0;
    for (int i = 0; i < s.length() - t.length() + 1; i++) {
        int c = 0;
        for (int j = i; j < i + t.length(); j++) {
            if (s[j] != t[j - i]) {
                c++;
            }
        }
        if (c < count || i == 0) {
            count = c;
        }
    }
    cout << count;
}
var s = readline();
var t = readline();
var count = 0;
for (var i = 0; i < s.length - t.length + 1; i++) {
    var c = 0;
    for (var j = i; j < i + t.length; j++) {
        if (s[j] != t[j - i]) {
            c++;
        }
    }
    if (c < count || i == 0) {
        count = c;
    }
}

console.log(count);

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *