Better ways to do this | Sololearn: Learn to code for FREE!

+2

Better ways to do this

I did this CodeCoach Deja Vu challenge and i feel like my approach is a bit clumsy,so i wanted to know about better and more efficient approaches. https://code.sololearn.com/c9t9Ez2wHd9e/?ref=app

1/28/2020 6:42:01 PM

Spandan Bhattacharya

12 Answers

New Answer

+4

If you have already Solved it then need to further discussion here. If you are getting any problem you can ask.

+3

With every i you only have to compare with the letters right from it (j=i+1).

+2

I don't think you need to put "put(a)" at the end

+1

+1

I used strchr and strrchr (in a for loop) from the string.h header file and compare what get returned...if they are different... then "Deja Vu".

+1

Hey, i think a better way is to create a array of char or string and sort it. If a string contains a duplicate than it will be set one by one gradually. Like "bacda" after sorting it becomes "aabcd". So it will be quite easy to find duplicate. And furthermore if you use Java than you have to try Hashset or LinkedHashset. Main use of this type of collections is it does not allow duplicates value.If you put a duplicate value in Hashset it will remove it. If you put "bacda" Hashset will return something like "bacd" if "bacda" ! ="bacd" //repeated else //norepeated

+1

+1

Remove puts(a) 21st line then you get exact result

0

0

i changed the double letter only problem,and now it is runs better,but your example will only search for repeats between 2 adjacent letters only.

0

yeah, that totally isn't a good solution even though it worked. i do it this way, first create an int array of 26 size where all of it is set to 0 where each index represents an alphabet. ex arr[0] is for a, arr[1] for b... the array value will represent the amount of alphabet so when you loop through the string, and found an 'a' then arr[0] will add by 1. after adding 1 on the index check if the value is 2, which means there are 2 'a' s already, then print deja vu if you reach the end of the string then print unique turning the character 'a' into 0 involves the use of ascii numbers, i suggest you study it. so the ascii of a = 97, b = 98. you just need to decrease the ascii value by 97 and you will return the index to store on your array. this is how i solve it and in my opinion the simplest way.

0

Yeah this is a nice method.