SoFunction
Updated on 2025-03-08

Detailed explanation of the first character in the output string that does not appear duplicate

Detailed explanation of the first character in the output string that does not appear duplicate

For example: input name output n, input teeter output r, input namename output null

The specific implementation code is as follows:

import ;

public class Main
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner();
    String str = ();
    for(int i =0 ; i < () ; i++)
    {
      if(((i)) == i && 
      ((i)) == i)
      {
        ((i));
        break;
      }
    }

  }
}

In this implementation code, we use three method members of the String class:

(): Get the length of the string
(int index): Get the character of the index index
(char c): Get the index of the last time the character c appears in a string
(char c): Get the index of the character c appears in the string for the first time

In fact, we can also implement it without using these methods of strings. The following is the code I personally use for loops to implement:

import ;
public class Main
{
  public static void main(String[] args)
  {
    Scanner in = new Scanner();
    String str = ();
    char[] cb = new char[()];
    //Save characters in the string into cb[] at one time    for(int i =0 ;i &lt;() ; i++)
    {
      cb[i] = (i);
    }
    for(int i = 0 ; i &lt; () ; i++)
    {
      for(int j = 0 ; j &lt; () ; j++)
      {
        if(cb[i] == cb[j] &amp;&amp; cb[i] != '0')
        {
          char c = cb[i];
          for(int z = 0 ; z &lt; () ; z++)
          {
            if(cb[z] == c)
              cb[z] = '0';
          }
        }
      }
    }
    for(int i = 0 ; i &lt;() ; i++)
    {
      if(cb[i] != '0')
      {
        (cb[i]);
        break;
      }
    }
  }
}

This method can be implemented, but the time complexity of this method is particularly large and the system overhead is also very large. Therefore, it is best not to use loop nesting, unless it is forced to do so, otherwise the system overhead is very large.

Thank you for reading, I hope it can help you. Thank you for your support for this site!