1
#include <bits/stdc++.h>

using namespace std;

int leftmost(string s) {
  string s1 = " ";
  int arr[256] = {0};
  for (int i = 0; i < s.length(); i++) {
    if (arr[int(s[i])] == 0) {
      arr[int(s[i])]++;
      s1 = s1 + s[i];
    }
    if (arr[int(s[i])] == 1) {
      arr[int(s[i])]++;
      int temp = s1.find(s[i]);
      s1.erase(temp, temp);
    }
  }
  return s.find(s1[0]);
}

int main() {
  string s = "geeksforgeeks";
  int res = leftmost(s);
  cout << res;
  return 0;
}

This is the code for leftmost non-repeating char in a string which is not giving correct output. It is giving output '1' while the expected output is 'f'. the output remains the same if we change the input string.

nickie
  • 5,608
  • 2
  • 23
  • 37
  • Side notes: https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h, https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice. – wohlstad Aug 15 '22 at 13:05
  • Please format your code with proper indentation and spacing. – wohlstad Aug 15 '22 at 13:06
  • Please don't use geekforgeeks (or any other so-called "competition" or "judge" site) as a learning or teaching resource. All it seems to teach are bad habits (like [that header file](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h)), bad code and sometimes even straight up *invalid* code. If you want to learn C++ properly, please invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) and take classes. – Some programmer dude Aug 15 '22 at 13:06
  • 2
    There seems to be a few logic errors in your code, but I guess the biggest is that your function `leftmost` returns an **index** but your `main` function assumes that it returns a **character**. So fix that bug first (which seems easy enough) but then you will find that there are still more bugs to fix. – john Aug 15 '22 at 13:07
  • 1
    In fact, `main` function also expects integer, so you can try `cout< – Akmal-threepointsix Aug 15 '22 at 13:08
  • @Someprogrammerdude You learn how to do competitive programming, though. – Ryan Zhang Aug 15 '22 at 13:09
  • Your usage of `std::string::erase` is incorrect. You are erasing `temp` characters starting at position `temp` doesn't seem like what you are trying to do. Additionally, it is suspect that `s1` starts with a space character rather than starting as an empty string. – François Andrieux Aug 15 '22 at 13:09
  • 1
    @RyanZhang Which is of no practical use anywhere else. – Some programmer dude Aug 15 '22 at 13:17
  • @Someprogrammerdude Not necessarily. When the focus is on the algorithm instead of the actual implementation, you can get a lot more done, a lot quicker. It's also useful for non-production code, say, if you're just writing code to test something (say, check if a certain conjecture holds). – Ryan Zhang Aug 15 '22 at 14:06
  • 1
    What you need to do is block GeeksForGeeks in your browser, and never go there again. After that, get a [good C++ book](https://stackoverflow.com/a/388282/4641116). – Eljay Aug 15 '22 at 14:42

0 Answers0