Check if two strings are same or not
Given two strings, the task is to check if these two strings are identical(same) or not. Consider case sensitivity.
Examples:
Input: s1 = “abc”, s2 = “abc”
Output: YesInput: s1 = “”, s2 = “”
Output: YesInput: s1 = “GeeksforGeeks”, s2 = “Geeks”
Output: No
Approach – By Using (==) in C++/Python/C#, equals in Java and === in JavaScript
This approach compares two strings to check if they are the same. It works by using the equality operator (
==
) to compare both strings character by character. If the strings are identical, it prints"Yes"
, otherwise, it prints"No"
. The comparison stops as soon as a mismatch is found, or if one string ends before the other. The program performs this comparison in a straightforward way without any extra steps, simply checking the content of the strings.
#include <bits/stdc++.h>
using namespace std;
// Function to compare both strings directly
bool areStringsSame(string s1, string s2) {
return s1 == s2;
}
int main()
{
string s1 = "abc";
string s2 = "abcd";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// Function to compare both strings directly
bool areStringsSame(const char* s1, const char* s2) {
return strcmp(s1, s2) == 0;
}
int main() {
const char* s1 = "abc";
const char* s2 = "abcd";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
printf("Yes\n");
} else {
printf("No\n");
}
return 0;
}
public class GfG {
// Function to compare both strings directly
public static boolean areStringsSame(String s1, String s2) {
return s1.equals(s2);
}
public static void main(String[] args) {
String s1 = "abc";
String s2 = "abcd";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
# Function to compare both strings directly
def areStringsSame(s1, s2):
return s1 == s2
if __name__ == '__main__':
s1 = "abc"
s2 = "abcd"
# Call the areStringsSame function to compare strings
if areStringsSame(s1, s2):
print("Yes")
else:
print("No")
using System;
class GfG {
// Function to compare both strings directly
static bool areStringsSame(string s1, string s2) {
return s1 == s2;
}
static void Main() {
string s1 = "abc";
string s2 = "abcd";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
// Function to compare both strings directly
function areStringsSame(s1, s2) {
return s1 === s2;
}
const s1 = "abc";
const s2 = "abcd";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
console.log("Yes");
} else {
console.log("No");
}
Output
No
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach – By Using String Comparison Functions
In this approach, the program compares two strings to check if they are identical. It uses the
strcmp
function, which compares the strings character by character. If the strings are exactly the same,strcmp
returns0
, indicating no difference between them. If the strings are different,strcmp
returns a non-zero value. Based on this result, the program prints"Yes"
if the strings match and"No"
if they don’t. The comparison stops as soon as a mismatch is found or the strings end.
#include <bits/stdc++.h>
using namespace std;
// Function to compare two strings using strcmp
bool areStringsSame(char s1[], char s2[]) {
return strcmp(s1, s2) == 0;
}
int main() {
char s1[] = "hello";
char s2[] = "hello";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// Function to compare two strings using strcmp
bool areStringsSame(char s1[], char s2[]) {
return strcmp(s1, s2) == 0;
}
int main() {
char s1[] = "hello";
char s2[] = "hello";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
printf("Yes\n");
} else {
printf("No\n");
}
return 0;
}
import java.util.Arrays;
class GfG {
// Function to compare two strings using equals
public static boolean areStringsSame(String s1, String s2) {
return s1.equals(s2);
}
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
# Function to compare two strings using ==
def areStringsSame(s1, s2):
return s1 == s2
s1 = "hello"
s2 = "hello"
# Call the areStringsSame function to compare strings
if areStringsSame(s1, s2):
print("Yes")
else:
print("No")
using System;
class GfG {
// Function to compare two strings using Equals
static bool areStringsSame(string s1, string s2) {
return s1.Equals(s2);
}
static void Main() {
string s1 = "hello";
string s2 = "hello";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
// Function to compare two strings using localeCompare
function areStringsSame(s1, s2) {
return s1.localeCompare(s2) === 0;
}
let s1 = "hello";
let s2 = "hello";
// Call the areStringsSame function to compare strings
if (areStringsSame(s1, s2)) {
console.log("Yes");
} else {
console.log("No");
}
Output
Yes
Time Complexity: O(n)
Auxiliary Space: O(1)
Approach – By Writing your Own Method
In this approach, the program compares two strings by first checking if their lengths are the same. If the lengths differ, the strings cannot be identical, so it returns
false
. If the lengths are the same, it then compares each character of the two strings one by one. If any mismatch is found, it returnsfalse
, indicating the strings are not the same. If no differences are found throughout the comparison, it returnstrue
, meaning the strings are identical.
#include <bits/stdc++.h>
using namespace std;
bool areStringsEqual(string &s1, string &s2) {
// Compare lengths first
if (s1.length() != s2.length()) {
return false;
}
// Compare character by character
for (size_t i = 0; i < s1.length(); ++i) {
if (s1[i] != s2[i]) {
return false;
}
}
return true;
}
int main() {
string s1 = "hello";
string s2 = "hello";
if (areStringsEqual(s1, s2)) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool areStringsEqual(const char *s1, const char *s2) {
// Compare lengths first
if (strlen(s1) != strlen(s2)) {
return 0;
}
// Compare character by character
for (size_t i = 0; i < strlen(s1); ++i) {
if (s1[i] != s2[i]) {
return 0;
}
}
return 1;
}
int main() {
const char *s1 = "hello";
const char *s2 = "hello";
if (areStringsEqual(s1, s2)) {
printf("Yes\n");
} else {
printf("No\n");
}
return 0;
}
public class GfG {
public static boolean areStringsEqual(String s1, String s2) {
// Compare lengths first
if (s1.length() != s2.length()) {
return false;
}
// Compare character by character
for (int i = 0; i < s1.length(); i++) {
if (s1.charAt(i) != s2.charAt(i)) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String s1 = "hello";
String s2 = "hello";
if (areStringsEqual(s1, s2)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
def are_strings_equal(s1, s2):
# Compare lengths first
if len(s1) != len(s2):
return False
# Compare character by character
for i in range(len(s1)):
if s1[i] != s2[i]:
return False
return True
s1 = "hello"
s2 = "hello"
if are_strings_equal(s1, s2):
print("Yes")
else:
print("No")
function areStringsEqual(s1, s2) {
// Compare lengths first
if (s1.length !== s2.length) {
return false;
}
// Compare character by character
for (let i = 0; i < s1.length; i++) {
if (s1[i] !== s2[i]) {
return false;
}
}
return true;
}
const s1 = "hello";
const s2 = "hello";
if (areStringsEqual(s1, s2)) {
console.log("Yes");
} else {
console.log("No");
}
Output
Yes
Time Complexity: O(n)
Auxiliary Space: O(1)