Check if two strings are anagrams

  Рет қаралды 9,838

CppNuts

CppNuts

Күн бұрын

JOIN ME
-----
KZbin 🎬 / @cppnuts
Patreon 🚀 / cppnuts
COMPLETE PLAYLIST
------------
C++ Tutorial For Beginners: • Introduction To C++
STL (Standard Template Library): • STL In C++
ThreadIng In C++: • Multithreading In C++
Data Structures: • Data Structure
Algorithms: • Binary Search
Design Patterns: • Factory Design Pattern...
Smart Pointers: • Smart Pointer In C++
C++14: • Digit Separator In C++
C++17: • std string_view in C++...
C++ All Type Casts: • static_cast In C++
INTERVIEW PLAYLIST
------------
C++ Interview Q&A: • Structural Padding & P...
C++ Interview Q&A For Experienced: • How delete[] Knows How...
Linked List Interview Questions: • Find Kth Node From Bac...
BST Interview Questions: • Search Element In Bina...
Array Interview Questions: • Reverse An Array
String Interview Questions: • Check String Is Palind...
Bit Manipulation Questions: • Find Set Bit In Intege...
Binary Tree Interview Question: • Invert Binary Tree
Sorting Algorithms: • Bubble Sort
C++ MCQ: • Video
C MCQ: • What printf returns af...
C Interview Questions: • Designated Initializat...
QUICK SHORT VIDEOS
-------------
C++ Short : • C++ Short Videos
C Short : • Shorts C Programming MCQ
In this video i have explained 3 different ways to solve this problem. This is very easy yet important question. This is generally asked in programming interview questions for placement. It can be best solved using hashing so interviewer ask this type of questions to check your knowledge on hashing.
Let me know if you feel something should be added in the comment section.
#anagram #dsa #c++ #interviewquestion

Пікірлер: 34
@pavankolli4490
@pavankolli4490 2 жыл бұрын
Boss is Back 😎
@CppNuts
@CppNuts 2 жыл бұрын
Thanks hahaha
@muhammadzakiahmad8069
@muhammadzakiahmad8069 7 ай бұрын
My python implementation: def is_Anagram(texta, textb): if len(texta) != len(textb): return False for char in texta: if texta.count(char) != textb.count(char): return False return True
@pitrya2533
@pitrya2533 2 жыл бұрын
or you can just add the ascii numbers and compare. say numbers a-z as 1-26 then string "car" will add up to 3+1+18 = 22 and "arc" will be the same.
@Anikashukla1808
@Anikashukla1808 5 ай бұрын
what will be the time complexity and space complexity for this?
@arpitagupta4997
@arpitagupta4997 5 ай бұрын
No , 10+2 = 8+4 = 12 for eg. This will lead to incorrect results
@SaurabhMishra0709
@SaurabhMishra0709 2 жыл бұрын
Hey buddy whr were u?? Please keep uploading videos...i really love your content...
@CppNuts
@CppNuts 2 жыл бұрын
Thanks man, sure I’ll do.
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 2 жыл бұрын
I guess we can avoid one additional loop, we can increment the count while iterating over first string and decrement the count of map for the second element. At the end we can iterate over the map if any all character has 0 count then it is anagram else it is not. Please correct me if I am wrong.
@CppNuts
@CppNuts 2 жыл бұрын
How r u populating Map which you are decrementing?
@kolyacpp
@kolyacpp 2 жыл бұрын
@@CppNuts we can delete key from map if after decrement Map[b[I]] becomes zero last loop can be replaced with: return Map.empty(); this method will be faster because decrement loop can break if there is no more that symbol in Map
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 2 жыл бұрын
@@CppNuts Since both string will be having same length , in that same for lopp we can increment the count of character from string 1 and decrement the count of the char for the other string.
@CppNuts
@CppNuts 2 жыл бұрын
Got it, yes that is correct, but in last solution we don't need 3rd loop that's what i asked in the end of the video. So in the end we have 2 loops in both the solutions. Good for you to think this much for given problem, then only you guys can get the max benefits.
@sairamsharma4111
@sairamsharma4111 2 жыл бұрын
How about having two sum variables and adding of ascii values for two string characters, if string length is same, in single for loop and comparing both sum are equal for an anagram
@CppNuts
@CppNuts 2 жыл бұрын
Hi Shriram, No this wont work.
@sairamsharma4111
@sairamsharma4111 2 жыл бұрын
@@CppNuts Yeah true👍 my bad one such example is "ac" & "bb"...thanks for reply!
@ChandraShekhar-by3cd
@ChandraShekhar-by3cd 2 жыл бұрын
Thanks a lot for your informative video. 😃
@CppNuts
@CppNuts 2 жыл бұрын
Thanks man!!
@notpipa_
@notpipa_ 2 жыл бұрын
Can i use two for loops to count each word characters count and then return true if map1 == map2?
@Anikashukla1808
@Anikashukla1808 5 ай бұрын
whats the time complexity and space complexity ?
@pallavichaudhary6636
@pallavichaudhary6636 Жыл бұрын
Thank you for the great content. It is helping to prepare for interviews. How can we make this solution, case insensitive. Currently it is case sensitive. If "Listen", "silent" - it's saying, not anagram. If "listen", "silent" - it's saying, anagram. Please suggest. We will have to check it's ascii value and convert ascii value to small case lettter asciis, I think and store that in the same map. Or at the beginning itself, convert both strings to lower case or upper case. Added below lines and it's working fine. #include //for transform function transform(one.begin(), one.end(), one.begin(), ::tolower); transform(two.begin(), two.end(), two.begin(), ::tolower); //where one and two are string variables
@CppNuts
@CppNuts Жыл бұрын
Converting in the beginning will be very easy, if interviewer is ok with that then grt, otherwise check what he/she wants in this case.
@arpittripathi488
@arpittripathi488 2 жыл бұрын
I want to know if that last loop is required?
@CppNuts
@CppNuts 2 жыл бұрын
Just think…
@arpittripathi488
@arpittripathi488 2 жыл бұрын
@@CppNuts I think it is not required. It can be avoided as the conditions above last loop cover all possible scenarios
@CppNuts
@CppNuts 2 жыл бұрын
Correct..
@HitanshSharma-q2l
@HitanshSharma-q2l Ай бұрын
Yes it is required reason: if the s1 contains some big string--> "abcd" and the s2 contains string like "abc" then even if the s1 left out with extra char "d", your code will return "true" for this... hence, if we have to check the i.second should be ==0
@duonghouc7184
@duonghouc7184 2 жыл бұрын
wonderful video
@ramakrishna4092
@ramakrishna4092 2 жыл бұрын
After long time
@CppNuts
@CppNuts 2 жыл бұрын
Yes next is vptr and vtable. Or i will complete this series first then will go for other topics.
@arpitjohari3784
@arpitjohari3784 2 жыл бұрын
@@CppNuts please make video for dll and move constructor
@badassopenpolling
@badassopenpolling 2 жыл бұрын
An important video.
@CppNuts
@CppNuts 2 жыл бұрын
Thanks
@cheelavaishnavi9125
@cheelavaishnavi9125 11 ай бұрын
how to do for more than 2 words like if cat tac act dog god we have to get act tac cat dog god
Check If Valid Parentheses
7:03
CppNuts
Рет қаралды 3,4 М.
Check if string is pangram | String Interview Question
5:02
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
She made herself an ear of corn from his marmalade candies🌽🌽🌽
00:38
Valja & Maxim Family
Рет қаралды 18 МЛН
Mom Hack for Cooking Solo with a Little One! 🍳👶
00:15
5-Minute Crafts HOUSE
Рет қаралды 23 МЛН
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 523 М.
What if all the world's biggest problems have the same solution?
24:52
Anagram | Check whether two strings are Anagram of each other | Java
10:46
B Tech Computer Science
Рет қаралды 42 М.
Check If Words Are Anagrams | C Programming Example
6:57
Portfolio Courses
Рет қаралды 13 М.
Writing Code That Runs FAST on a GPU
15:32
Low Level
Рет қаралды 585 М.
Check If A String Is A Palindrome | C Programming Example
10:56
Portfolio Courses
Рет қаралды 45 М.
Dependency Injection, The Best Pattern
13:16
CodeAesthetic
Рет қаралды 921 М.
Check Two Strings are Anagram Or Not using Sort and Equals in Java
11:10
Naveen AutomationLabs
Рет қаралды 34 М.