Przejdź do głównej zawartości

Posty

Wyświetlam posty z etykietą for

String-2 > xyBalance

We'll say that a String is xy-balanced if for all the 'x' chars in the string, there exists a 'y' char somewhere later in the string. So "xxy" is balanced, but "xyx" is not. One 'y' can balance multiple 'x's. Return true if the given string is xy-balanced. xyBalance("aaxbby") → true xyBalance("aaxbb") → false xyBalance("yaaxbb") → false Szukam char i rownego x, nastepnie musimy spwardzic czy po tym x jest y tj generujemy string i drugi warunek ten substring zaczyna sie od i w ktorym zostal odnotowany x i jedzie do konca szukajac y - jesli true to oba true jesli false wtedy false public boolean xyBalance ( String str ) { for ( int i = 0 ; i < str . length (); i ++){ if ( str . charAt ( i )== 'x' ){ if ( str . substring ( i + 1 , str . length ()). contains ( "y" )&& str . charAt ( str . length ()- 1 )!= 'x' ){ ...

String-2 > xyzThere - java

Return true if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period (.). So "xxyz" counts but "x.xyz" does not.  xyzThere("abcxyz") → true xyzThere("abc.xyz") → false xyzThere("xyz.abc") → true Definiujemy loop ktory sprawdza za kazdym podejsciem czy kolejne indexy i,i+1 oraz i+2 i zdefiniowane dla nich char.  Nalezy tu pamietac ze jesli sprawdamy po indeksach np i+2 to tzreba zostawic "miejsce" na koncu aby nie bylo outOfBoudnExeption tj przekroczenia rlugosci stringa. Jezeli pierwszy warunek jest spelniony tj mamy na kolejnych indexach interesujace nas char-y sprawdzamy czy na poprzedzajacych nasza trojkę indexach pojawia sie "." zaczynamy od indexu 0 - tzreba to uwzglednić w warunku tj albo index 0 == 0 lub i-1 == 0 public boolean xyzThere ( String str ) { int len = str . length () - 2 ; for ( int i = 0 ; i < len ; i...