Given two strings, 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
Table of Content
Using (==) in C++/Python/C#, equals in Java and === in JavaScript - O(n) Time and O(1) Space
The idea is to use equality operator (==) to compare both strings character by character. If the strings are identical, it prints "Yes", otherwise, it prints "No".
#include <iostream>
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";
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";
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";
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"
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";
if (areStringsSame(s1, s2)) {
Console.WriteLine("Yes");
} else {
Console.WriteLine("No");
}
}
}
// Function to compare both strings directly
function areStringsSame(s1, s2) {
return s1 === s2;
}
//Driver Code
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
Using String Comparison Functions - O(n) Time and O(1) Space
The idea is to the direct string comparison function, which compares the strings character by character. If the strings are exactly the same, it returns 0.. If the strings are different,, 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 <iostream>
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
def main():
s1 = "hello"
s2 = "hello"
# Call the areStringsSame function to compare strings
if areStringsSame(s1, s2):
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
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;
}
//Driver Code
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
Writing your Own Method - O(n) Time and O(1) Space
- If their lengths are the same. If the lengths differ, the strings cannot be identical, so it returns false.
- Then compares each character of the two strings one by one. If any mismatch is found, it returns false.
- If no differences are found throughout the comparison, it returns true.
#include <iostream>
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
def main():
s1 = "hello"
s2 = "hello"
if are_strings_equal(s1, s2):
print("Yes")
else:
print("No")
if __name__ == "__main__":
main()
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;
}
//Driver Code
const s1 = "hello";
const s2 = "hello";
if (areStringsEqual(s1, s2)) {
console.log("Yes");
} else {
console.log("No");
}
Output
Yes