How can I iterate over a string in Java?

前端 未结 9 1778
余生分开走
余生分开走 2021-01-02 12:40
public static Boolean cmprStr( String s1, String s2 )
{
    // STUFF
}

I want to iterate through s1 to make sure that every character in s1 is incl

9条回答
  •  情书的邮戳
    2021-01-02 13:28

    All the other answers are O(n^2). Here's a way that is linear in time (i.e. O(n)) using Google Guava:

      public static boolean cmprStr(String s1, String s2) {
        Set desiredCharacters = Sets.newHashSet(Lists.charactersOf(s2));
        return Sets.difference(Sets.newHashSet(Lists.charactersOf(s1)), desiredCharacters).isEmpty();
      }
    

提交回复
热议问题