Same characters in two strings | Practice | GeeksforGeeks

Given two strings A and B of equal length, find how many times the corresponding position in the two strings hold exactly the same character. The comparison should not be case sensitive. 
Example 1:
Input:
A = choice 
B = chancE
Output: 4
Explanation: characters at position 0, 1, 4 and 5
are same in the two strings A and B.
Example 2:
Input:
A = Geek 
B = gang
Output: 1
Explanation: charactera at position 0 is the
same in the two strings A and B.

Your Task:  
You dont need to read input or print anything. Complete the function sameChar() which takes the two strings A and B as input parameters and returns the count of the characters that are same in A and B.

Expected Time Complexity: O(N) where N is the length of strings A and B.
Expected Auxiliary Space: O(1) 

Constraints:
1<= A.length(), B.length() <= 104
 
0
mehtay0374 days ago
Python Solution:
class Solution:
   def sameChar(self, A, B):       # code here       a=A.lower()       b=B.lower()       count=0       for i in range(len(a)):           if(a[i]==b[i]):               count+=1       return count         
0
debasishtewary51 week ago
 EASY SOLUTION
int sameChar(String A, String B)    {       // code here       int c=0;for(int i=0;i<A.length();i++){   if((int)A.charAt(i)==(int)B.charAt(i)||(int)A.charAt(i)==(int)B.charAt(i)+32||(int)A.charAt(i)+32==(int)B.charAt(i)){       c++;   }}      return c;
+1
badgujarsachin835 months ago
int sameChar(string A, string B)
    { 
        int count=0;
        for(int i=0;i<A.size();i++){
            if(tolower(A[i])==tolower(B[i])){
                count++;
            }
        }
        return count;
    }
0
Imran Wahid7 months ago
Imran Wahid
Easy C++ solution
class Solution{    public:    int sameChar(string A, string B)    {        int count=0;        for(int i=0;i<a.length();i++) <br=""/>        {            if(A[i]>='A' && A[i]<='Z')            {                A[i]+=32;            }            if(B[i]>='A' && B[i]<='Z')            {                B[i]+=32;            }            if(A[i]==B[i])            {                count++;            }        }        return count;    }};
0
Priyam Saxena10 months ago
Priyam Saxena
class Solution {     int sameChar(String A, String B)     {        int c=0;        A = A.toLowerCase();        B = B.toLowerCase();        for(int i=0;i<a.length();i++) {="" if(a.charat(i)="=B.charAt(i))" {="" c++;="" }="" }="" return="" c;="" }="" }="">
0
Annanya Mathur1 year ago
Annanya Mathur
#include<bits stdc++.h="">using namespace std;int main() {int t;cin>>t;while(t--){    string a,b;    cin>>a>>b;    int c=0;    for(int i=0;i<a.size();i++) {="" if(isupper(a[i]))="" a[i]="tolower(a[i]);" if(isupper(b[i]))="" b[i]="tolower(b[i]);" if(a[i]="=b[i])" c++;="" }="" cout<<c<<endl;="" }="" return="" 0;="" }="">
0
Aman Vishwakarma1 year ago
Aman Vishwakarma
int main() {    int t;    cin>>t;    while(t!=0)    {        int count=0;        string s1,s2;        cin>>s1;        cin>>s2;        for(int i=0;i<s1.length();i++) {="" if(s1[i]="=s2[i]" ||="" s1[i]="=(char)(s2[i]+32)" ||="" s1[i]="=(char)(s2[i]-32))" count++;="" }="" cout<<count="" <<"\n";="" t--;="" }="" return="" 0;="" }="">
0
Siddharth Katiyar1 year ago
Siddharth Katiyar
#include<bits stdc++.h="">using namespace std;int main(){    int t;    cin >> t;    while(t--){        string s1;        string s2;        cin >> s1;        cin >> s2;         transform(s1.begin(), s1.end(), s1.begin(), ::tolower);        transform(s2.begin(), s2.end(), s2.begin(), ::tolower);         int i = 0, j = 0, count = 0;        while (s1.length() && j < s2.length())        {            if (s1[i] == s2[j])                count++;            i++;j++;        }         cout << count << endl;    }    return 0;}
0
shivendu1 year ago
shivendu
#include<bits stdc++.h=""> using namespace std;int cpostion(string S1,string S2){int count=0;int i=0;int j=0;    transform(S1.begin(), S1.end(), S1.begin(), ::toupper);         transform(S2.begin(), S2.end(), S2.begin(), ::toupper);     while( i<s1.length() &&="" j<s2.length())="" {="" if(s1[i]="=" s2[j])="" {="" count++;="" }="" i++;="" j++;="" }="" return="" count;="" }="" int="" main()="" {="" int="" t;="" cin="">>t;while(t--){    string s1;     string s2;    cin>>s1;    cin>>s2;     cout<<cpostion(s1,s2)<<endl; }="" }="">
0
pratyush shekhar1 year ago
pratyush shekhar
t=int(input())for i in range(t):    s1=input()    s2=input()    s1=s1.lower()    s2=s2.lower()    count=0    for i in range(len(s1)):        if(s1[i]==s2[i]):            count=count+1    print(count)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.