[Algorithm] 문자열 Substring, CompareTo⭐

업데이트:

카테고리:

태그: ,


str.Substring, str1.CompareTo(str2)



Algorithm

Algorithm








1. Algorithm

  • Substring(i, p.Length)
  • strA.CompareTo(p)
public int solution(string t, string p) {
        int answer = 0;
        for(int i = 0; i < t.Length-p.Length+1; i++)
        {
            string strA = t.Substring(i, p.Length);
            int compare = strA.CompareTo(p);
            if (compare<=0) answer++;
        }
        return answer;
    }
  • 예) t = “123”, p=”2”

t.Substring(i, p.Length)

  • t의 i번째부터 p.Length(1) 개만큼 가져와서 strA에 저장.


strA.CompareTo(p)

  • strA와 P를 비교한다
  • strA가 크면 +1
  • 같으면 0
  • 작으면 -1

잡담

알고리즘에 문자열이 나올때는 항상 필요한 기능을 찾아본다. 정리해서 기억하고 있으면 좋을 거 같다.




[Algorithm] Algorithm


참고 : 유니티 TOP


📔

댓글남기기