+ 2

How to check if two characters inside one string are the same? - Answered, thank you!!

How do I do it? Could not find a satisfying answer online... It is driving me insane! Help.

29th Jun 2020, 1:45 PM
The_Fox
The_Fox - avatar
33 odpowiedzi
+ 4
Use floyd's tortoise and hare algorithm. A simple cycle detection algorithm that has one point that traverses as twice as another pointing to nodes in our case the indices of the string, since strings can be indexed just like lists in python or arrays for other programming languages. Since there are going to meet somewhere there is going to be a cycle , find that cycle and we'll have an answer. And the goodness is that it works in 0(n) space and 0(1) time.
30th Jun 2020, 8:18 PM
Mwikisa Kapesa Lufungulo
Mwikisa Kapesa Lufungulo - avatar
+ 3
If you are allowed to alter the original string, then sorting the characters in the string may help you in shortening the time needed in finding duplicate character.
29th Jun 2020, 2:03 PM
Ipang
+ 3
Share your code link in your thread Description, let's see what we can find ...
29th Jun 2020, 2:06 PM
Ipang
+ 3
If I enter Ituuu it works fine and prints: TRUE TRUE FALSE
29th Jun 2020, 5:32 PM
Alexander Thiem
Alexander Thiem - avatar
+ 3
Or you can replace break with abort() to completely end the programm and move the cout<<„True“; Statement to after the for loop. You see there are lots of options to improve this last issue
29th Jun 2020, 5:48 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
If you use a char* you just can access both and compare them....
29th Jun 2020, 1:50 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
I need a break first... Will send the code when I‘m back, thank you!
29th Jun 2020, 2:10 PM
The_Fox
The_Fox - avatar
29th Jun 2020, 2:19 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
First of all, thank you :) And yes, I find it a little difficult because I have an information overload today and can‘t figure out the simplest of tasks as it seems. To check if a char at i == char at i+1 I just need to write sth. like: if (string[i] == string[i+1]? See, little things like char as opposed to string and the way they are used for different things confuse me still. Or do you mean if (str.at(i) == str.at(i+1)) ??
29th Jun 2020, 4:24 PM
The_Fox
The_Fox - avatar
+ 2
I would suggest you not to print the result in the for loop... better use the for loop for calculating whether you should print True or false and print it after the for loop
29th Jun 2020, 5:40 PM
Alexander Thiem
Alexander Thiem - avatar
+ 2
You can use std::unique in <algorithm> string a; auto e=std::unique(a.begin(),a.end()); e is equal to a.end() if all elements are unique.Otherwise,it points to the first duplicate element
29th Jun 2020, 6:17 PM
Anthony Maina
Anthony Maina - avatar
+ 2
Great, thanks!
29th Jun 2020, 6:56 PM
The_Fox
The_Fox - avatar
+ 2
👍 You made it
30th Jun 2020, 7:58 AM
Alexander Thiem
Alexander Thiem - avatar
+ 2
Andrebot AstRein Maybe you mark your question as solved if you want to avoid more and more answers like from Noman and Ameer Haider, who post after your code works...
30th Jun 2020, 7:09 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
Will try :) Thank you.
29th Jun 2020, 1:52 PM
The_Fox
The_Fox - avatar
+ 1
An example? Does letter case matter? Alphabet only or any character?
29th Jun 2020, 1:53 PM
Ipang
+ 1
It is the Isogram challenge. I need to check if any character in a string repeats. E.g. bool is not an isogram but hour is.
29th Jun 2020, 1:55 PM
The_Fox
The_Fox - avatar
+ 1
You are welcome
29th Jun 2020, 1:56 PM
Alexander Thiem
Alexander Thiem - avatar
+ 1
I tried that, sorted the string, created a nested loop but still couldn‘t get what I wanted... frustrating! I‘m still trying right now, using the str.find method.
29th Jun 2020, 2:04 PM
The_Fox
The_Fox - avatar
+ 1
Ok, so I sorted a string to , let‘s say, “eHllo”. Now I want to check if any character occurs twice in the string. I think I am making things too complicated... Do I really have to use a loop for this task? Is there a way around it using pointers somehow??
29th Jun 2020, 3:34 PM
The_Fox
The_Fox - avatar