[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


๐Ÿ“”

๋Œ“๊ธ€๋‚จ๊ธฐ๊ธฐ